115th LeetCode Weekly Contest 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 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:
cells.length == 8
cells[i]
is in{0, 1}
1 <= N <= 10^9
如果知道有规律,那么就可以好解,只需要暴力跑一次找出规律就行。这题还真的是有规律,就是14天一次的规律
class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int N) {
int len = cells.size();
int num[];
int x = N% == ? : N%;
for(int i=;i<x;i++){ for(int j=;j<=;j++){
num[j] = cells[j-]+cells[j+];
}
for(int j=;j<=;j++){
if(num[j]==||num[j]==){
cells[j]=;
}else{
cells[j]=;
}
}
if(cells[]==){
cells[]=;
}
if(cells[]==){
cells[]=;
}
}
//89
//96
//14
//
return cells;
}
};
115th LeetCode Weekly Contest Prison Cells After N Days的更多相关文章
- 115th LeetCode Weekly Contest Check Completeness of a Binary Tree
Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- 【LeetCode】957. Prison Cells After N Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
随机推荐
- IDEA03 连接数据库、自动生成实体类
1 版本说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 IDEA:旗舰版207.2 MySQL:5.5 2 利用IDEA连接数据库 说明:本案例以MySQL为例 2.1 打 ...
- linq 条件查询与分页
<div>姓名:<asp:TextBox ID="T1" runat="server"></asp:TextBox>< ...
- Tensorflow学习(练习)—CPU训练模型
Mask R-CNN - Train on Shapes Dataset This notebook shows how to train Mask R-CNN on your own dataset ...
- 30-懒省事的小明(priority_queue)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=55 懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 ...
- while 循环和do while循环
while循环是先检测条件符合不符合,符合才执行循环体内容,不符合就跳过while循环. 就和一个房间有两个门,一个前门,一个后门,while循环是当你进入前门的时候有人会检查你的身份,只有身份符合条 ...
- 使用 insertBefore 和insertAfter,在指定位置追加与删除元素
来自于<sencha touch 权威指南> ----------------------------------- 除 append 和 overwrite 外,还可以使用 insert ...
- 数据库MySQL 之 库操作
数据库MySQL 之 库操作 浏览目录 系统数据库简介 数据库操作 命名规范 用户权限 修改密码 忘记密码 中文乱码问题 一.系统数据库简介 information_schema :虚拟库,不占用磁盘 ...
- python 正则表达式 练习题
会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab.abbbb 等 * 前面元素出现0次或多次 ab*:a.ab.abb 等 ? 匹配前面的一次或0次 Ab?: A.Ab 等 ...
- 几个SQL小知识(转)
出处:http://www.cnblogs.com/wuguanglei/p/4205976.html 写在前面的话:之前做的一个项目,数据库及系统整体构架设计完成之后,和弟兄们经过一段时间的编码,系 ...
- c# .NET开发邮件发送功能的全面教程(含邮件组件源码)
http://www.cnblogs.com/heyuquan/p/net-batch-mail-send-async.html