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. persistence.xml模板配置

    1.右键创建的persistence.xml 2.选择2.0版本的模板 3.复制右侧代码,写入xml文件中 <?xml version="1.0" encoding=&quo ...

  2. 异常-Data truncation: Truncated incorrect DOUBLE value: '-9370.3530-'

    1详细异常日志 9/11/04 17:36:09 ERROR base.SQLHelper: Data truncation: Truncated incorrect DOUBLE value: '- ...

  3. 6.caffe:create_txt.sh(数据预处理成txt文本文件)

    #!/usr/bin/env sh DATA=/home/wp/CAFFE/caffe-master/myself/00b MY=/home/wp/CAFFE/caffe-master/myself/ ...

  4. FlexPaper的深入了解和应用

    作者:tabb_ 零下疯度 推荐:无痕客 最近做项目需要用到flexpaper,所以想借此机会好好的研究一下. 这是官方的下载地址:http://flexpaper.devaldi.com/downl ...

  5. maven热部署插件-jetty

    作者:小勇Oo 关于maven-jetty-plugin的说明: pom文件中: <build> <finalName>freemarker</finalName> ...

  6. [MySQL优化] -- 如何查找SQL效率地下的原因

    [MySQL优化] -- 如何查找SQL效率地下的原因   来源: ChinaUnix博客 日期: 2009.07.20 16:12 (共有条评论) 我要评论       查询到效率低的 SQL 语句 ...

  7. hive数据仓库表设计之(矮宽表+高窄表)

    昨天面对某客户域做表关联的时候发现了. 有两张相同内容的主表.但是表的设计结构并不相同: (每个领域都有主表,每次往这个领域(库)添加新表的时候一般都会join 主表,从而有唯一的主键id) 这两个表 ...

  8. scrollTop([val])

    scrollTop([val]) 概述 获取匹配元素相对滚动条顶部的偏移.广州大理石机械构件 此方法对可见和隐藏元素均有效. 参数 valString, NumberV1.2.6 设定垂直滚动条值 示 ...

  9. luogu 3047 [USACO12FEB]附近的牛Nearby Cows 树形dp

    $k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 ...

  10. luogu SP8093 后缀自动机+树状数组+dfs序

    这题解法很多,简单说几个: 1. 线段树合并,时间复杂度是 $O(nlog^2n)$ 的. 2. 暴力跳 $fail,$ 时间复杂度 $O(n\sqrt n),$ 比较暴力. 3. 建立后缀树后在 $ ...