LC 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 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
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]
题目要求:8个(0,1)一排,两边相同中间变1,两边不同中间变0。问N次后的数组样子。
思路1:使用字典记录每一个过程和遍历时的N,如果有重复直接取模。减少运算量。
class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int N) {
unordered_map<string, int> map;
string firstcell = "";
for (int i = ; i<cells.size(); i++) {
firstcell += to_string(cells[i]);
}
while (N != ) {
if (map.count(firstcell))
N %= map[firstcell] - N;
if(N == ) break;
string nextstr = "";
for (int i = ; i < ; i++) {
nextstr += firstcell[i - ] == firstcell[i + ] ? "" : "";
}
nextstr = "" + nextstr + "";
//cout << nextstr << endl;
map[firstcell] = N;
firstcell = nextstr;
N--;
}
vector<int> ret;
for (int i = ; i<firstcell.size(); i++) {
if (firstcell[i] == '') ret.push_back();
else ret.push_back();
}
return ret;
}
};
LC 957. Prison Cells After N Days的更多相关文章
- 【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, wheth ...
- 【LeetCode】957. Prison Cells After N Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...
- [Swift]LeetCode957. N天后的牢房 | 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 ...
- 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 ...
- weekly contest 115
958. Check Completeness of a Binary Tree Given a binary tree, determine if it is a complete binary t ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- English trip V1 - 1.How Do You Feel Now? Teacher:Lamb Key:形容词(Adjectives)
In this lesson you will learn to describe people, things, and feelings.在本课中,您将学习如何描述人,事和感受. STARTER ...
随机推荐
- 7.Spring整合Hibernate_1
Spring 整合 Hibernate 1.Spring指定 database,给下面创建的 SessionFactory用 <!-- !!!!!可以使用 @Resource 将 这个bean对 ...
- C#编程 LINQ查询
LINQ查询表达式 约束 LINQ查询表达式必须以from子句开头,以select或group子句结束 关键字 from...in...:指定要查找的数据以及范围变量,多个from子句则表示从多个数据 ...
- 寻找一组数中最大的K个数
对于"从一组数中挑出最大的K个数"这个在面试中经常会遇到,所以这次好好的去解析它,而当拿到这个问题时第一时间能想到解法就是:先对数据进行排序,然后再取最大的K个元素,当然这思路没毛 ...
- go语言日期时间格式化
原文: http://www.golangprograms.com/get-current-date-and-time-in-various-format-in-golang.html package ...
- Switch按钮
使用CSS+HTML5修改原生checkbox为Switch Button .switch { width: 45px; height: 15px; position: relative; borde ...
- python_函数参数
1.参数的基本知识 任意个数 任意类型 def func(a1,a2,a3): print(a1,a2,a3) # 参数可以是任意个数和任意类型 func(1,'waf',True) 2.位置传参数( ...
- metal2 里 programmable blending 和image block的区别 语法以及persistent thread group的语法
programmable blending 刚接触这个概念的时候 挺激动的 因为能解决很多管线里面的问题 比如 切一次rt再切回来 为了做read write same rt 有了这个 就不用切啦 可 ...
- MySQL分组查询,查询出某一个字段的最新记录
直接上案例...... 案例: 同一个表中,只想需要A.B.C的最新记录 第一种方案: 应该还很多方法......(暂时先这样.....)
- 使用WebSocket实现简单的在线聊天室
前言:我自已在网上找好了好多 WebSocket 制作 在线聊天室的案列,发现大佬们写得太高深了 我这种新手看不懂,所以就自已尝试写了一个在线简易聊天室 (我只用了js 可以用jq ) 话不多说,直接 ...
- 基于IAP的STM32程序更新技术
引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...