题目如下:

There are 8 prison cells in a row, and each cell is either occupied or vacant.

Each day, whether the cell is occupied or vacant changes according to the following rules:

  • If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
  • Otherwise, it becomes vacant.

(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)

We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.

Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)

Example 1:

Input: cells = [0,1,0,1,1,0,0,1], N = 7
Output: [0,0,1,1,0,0,0,0]
Explanation:
The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]

Example 2:

Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]

Note:

  1. cells.length == 8
  2. cells[i] is in {0, 1}
  3. 1 <= N <= 10^9

解题思路:当我看到第二个用例中 N = 1000000000 后,直觉告诉我变换结果中应该存在周期性的循环。所以我就测试了前100天结果,发现这个周期是14天。这也是是一种取巧的方法了。

代码如下:

class Solution(object):def prisonAfterNDays(self, cells, N):""":type cells: List[int]:type N: int:rtype: List[int]"""if N != 0:N = N % 14 if N % 14 != 0 else 14while N > 0:tl = [0]for i in range(1,len(cells)-1):if cells[i-1] == cells[i+1]:tl.append(1)else:tl.append(0)tl.append(0)cells = tl[:]N -= 1return cells

转载于:https://www.cnblogs.com/seyjs/p/10135651.html

【leetcode】957. Prison Cells After N Days相关推荐

  1. 【LeetCode】957. N 天后的牢房

    文章目录 题目:957. N 天后的牢房 解题思路 代码 题目:957. N 天后的牢房 957. N 天后的牢房 8 间牢房排成一排,每间牢房不是有人住就是空着. 每天,无论牢房是被占用或空置,都会 ...

  2. 【LeetCode】957.N天后的牢房

    题目描述: 8 间牢房排成一排,每间牢房不是有人住就是空着. 每天,无论牢房是被占用或空置,都会根据以下规则进行更改: 如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用. 否则,它 ...

  3. 【leetcode】1030. Matrix Cells in Distance Order

    题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...

  4. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  5. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  6. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  7. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  8. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  9. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

最新文章

  1. IntelliJ IDEA 2019 快捷键终极大全,速度收藏!
  2. spring coud feign
  3. 对php专业的认识,对PHP要有个全面的认识
  4. informix和mysql数据量_informix数据库知识积累
  5. 《结对-结对编项目作业名称-测试过程》
  6. mysql关系数据库引擎_MySQL数据库引擎详解
  7. printf函数源码linux,再来一版简易的printf函数实现
  8. Akamai “三驾马车”,如何应对疫情后新场景形态下的新考验?
  9. Spring boot实体类中常用基本注解
  10. azure云数据库_Azure SQL数据库地理复制
  11. 查oracle执行的sql,oracle查询正在执行的sql
  12. 为什么会自动打开Nautilus文件管理器?
  13. GPCP全球月降水量数据下载与读取
  14. STM8L RTC总结初始化和配置
  15. Android预览Office文档
  16. php运维知识,分享一些linux运维的基础知识
  17. 2020高德技术年刊:18万字、750页+,智慧出行最佳技术实践都在这了
  18. win7 IE11浏览器怎么降至IE8?
  19. NETCTOSS代码实现第五版
  20. python 写入excel数字格式_从Pandas写入Excel时设置默认数字格式

热门文章

  1. Fedora16 下更改多系统、多内核的默认启动项
  2. 初识PHP变量函数语法
  3. jQuery零基础入门——(三)层级选择器
  4. 蓝鲸“配置平台”正式开源
  5. 学android开发,入门语言JAVA知识点
  6. Servlet Filter
  7. DKIM标准:对付网络钓鱼的新武器
  8. virtio+ovs转发原理和性能分析
  9. 容器赋能AI-人工智能在360私有云容器服务上的实践
  10. netty 水位线与oom