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的更多相关文章

  1. 【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 ...

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

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

  3. [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 ...

  4. 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 ...

  5. weekly contest 115

    958. Check Completeness of a Binary Tree Given a binary tree, determine if it is a complete binary t ...

  6. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 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. ...

  9. 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  ...

随机推荐

  1. 运行tomcat7w.exe提示指定的服务未安装 解决办法

    一.问题重现点击bin下tomcat7w.exe出现如下提示:提示指定的服务未安装 二.原因分析tomcat7.exe和tomcat7w.exe要起作用必须先为这两个文件安装服务.其中tomcat7. ...

  2. 解决docker容器开启端口映射后,会自动在防火墙上打开端口的问题

    在docker中运行第三方服务时,通常需要绑定服务端口到本地主机.但使用 -p 参数进行的端口映射,会自动在iptables中建立规则,绕过firewalld,这对于端口级的黑白名单控制管理是很不利的 ...

  3. 13_Redis_持久化

    一:概述: Redis的高性能是山于其将所有数据都存储在了内存中,为了使Redis在重启之后仍能保证数据不丢失,需要将数据从内存中同步到硬盘中,这一过程就是持久化. Redis支持两种方式的持久化,一 ...

  4. 2. An Array of Sequences

    1. Overview of Built-In Sequences Container sequences: list, tuple, and collections.deque can hold i ...

  5. Ubuntu系统---WeChat安装

    Ubuntu安装微信教程 工具/原料 ubuntu 14.04 x86 方法/步骤 这次我用的系统是Ubuntu 14.04 x86,在网上先去下载electronic-wechat-linux ht ...

  6. C++STL库常用函数用法

    开学就要上OOP了.....感觉十分萌萌哒- -! 整理自<ACM程序设计>,本文为转载(原文地址) 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组 ...

  7. 洛谷P2664 树上游戏——点分治

    原题链接 被点分治虐的心态爆炸了 题解 发现直接统计路径上的颜色数量很难,考虑转化一下统计方式.对于某一种颜色\(c\),它对一个点的贡献为从这个点出发且包含这种颜色的路径条数. 于是我们先点分一下, ...

  8. [唐胡璐]Selenium技巧- ReportNG替换TestNG默认结果报告

    TestNG默认的报告虽然内容挺全,但是展现效果却不太理想,不易阅读。因此我们想利用ReportNG来替代TestNG默认的report。 什么是ReportNG呢?这里不多说,请直接参见:http: ...

  9. css hack的理解

    什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6-IE11,Firefox/Safari/Opera/Chrome等),对CSS的支持.解析不一样,导致在不同浏览器的环境中呈 ...

  10. CSS3 -- column 实现瀑布流布局

    本例使用 CSS column 实现瀑布流布局 关键点,column-count: 元素内容将被划分的最佳列数 关键点,break-inside: 避免在元素内部插入分页符 html div.g-co ...