【leetcode】957. Prison Cells After N Days
题目如下:
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 thei
-th cell is occupied, elsecells[i] == 0
.Given the initial state of the prison, return the state of the prison after
N
days (andN
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:
cells.length == 8
cells[i]
is in{0, 1}
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相关推荐
- 【LeetCode】957. N 天后的牢房
文章目录 题目:957. N 天后的牢房 解题思路 代码 题目:957. N 天后的牢房 957. N 天后的牢房 8 间牢房排成一排,每间牢房不是有人住就是空着. 每天,无论牢房是被占用或空置,都会 ...
- 【LeetCode】957.N天后的牢房
题目描述: 8 间牢房排成一排,每间牢房不是有人住就是空着. 每天,无论牢房是被占用或空置,都会根据以下规则进行更改: 如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用. 否则,它 ...
- 【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 ...
- 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题
[LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【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 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
最新文章
- IntelliJ IDEA 2019 快捷键终极大全,速度收藏!
- spring coud feign
- 对php专业的认识,对PHP要有个全面的认识
- informix和mysql数据量_informix数据库知识积累
- 《结对-结对编项目作业名称-测试过程》
- mysql关系数据库引擎_MySQL数据库引擎详解
- printf函数源码linux,再来一版简易的printf函数实现
- Akamai “三驾马车”,如何应对疫情后新场景形态下的新考验?
- Spring boot实体类中常用基本注解
- azure云数据库_Azure SQL数据库地理复制
- 查oracle执行的sql,oracle查询正在执行的sql
- 为什么会自动打开Nautilus文件管理器?
- GPCP全球月降水量数据下载与读取
- STM8L RTC总结初始化和配置
- Android预览Office文档
- php运维知识,分享一些linux运维的基础知识
- 2020高德技术年刊:18万字、750页+,智慧出行最佳技术实践都在这了
- win7 IE11浏览器怎么降至IE8?
- NETCTOSS代码实现第五版
- python 写入excel数字格式_从Pandas写入Excel时设置默认数字格式