题目如下:

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 14
while 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 -= 1
return cells

【leetcode】957. Prison Cells After N Days的更多相关文章

  1. 【LeetCode】957. Prison Cells After N Days 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...

  2. 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  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】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. 玩玩Mybatis的逆向工程

    通过数据库 逆向生成代码 主要配置的文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  2. Android processDebugManifest 异常

    1.使用 gradlew processDebugManifest --stacktrace 进行排查; 2.异常: processDebugManifest (Thread[Execution wo ...

  3. I2C自编设备驱动设计

    一.自编设备驱动模型 at24.c: static int __init at24_init(void) { io_limit = rounddown_pow_of_two(io_limit); re ...

  4. <自动化测试>之<selenium API 查找元素操作底层方法>

    搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏driver = webdriver.Chrome(); 打开测试页面 driver.get( "http://b ...

  5. 探索Redis设计与实现2:Redis内部数据结构详解——dict

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  6. 【c#技术】一篇文章搞掂:常见C#技术问题

    1.事件作为参数传递 public class Para { // 定义一种委托(事件类型),可以在此定义这个事件的返回值和参数 public delegate object GetDataMetho ...

  7. JavaWeb解决中文乱码

    1.Get请求,方案有两种 A:修改Tomcat配置文件 server.xml   URIEncoding="UTF-8" 如:<Connector port="8 ...

  8. EditText的常用点,输入控制(包含inputType)

    部分内容参考:http://www.cnblogs.com/hnrainll/archive/2012/11/24/2786159.html 控制EditText的输入的时候: 1)常用设置 andr ...

  9. How do I force my .NET application to run as administrator?

    How do I force my .NET application to run as administrator? You'll want to modify the manifest that ...

  10. linux中软连接和硬链接的区别

    linux中创建软连接和硬链接的方法: 软连接: ln -s oldfile slink 硬连接: ln oldfile hlink linux中创建软连接和硬链接的区别:        原理上,硬链 ...