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
  2. Output: [0,0,1,1,0,0,0,0]
  3. Explanation:
  4. The following table summarizes the state of the prison on each day:
  5. Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
  6. Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
  7. Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
  8. Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
  9. Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
  10. Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
  11. Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
  12. Day 7: [0, 0, 1, 1, 0, 0, 0, 0]

题目要求:8个(0,1)一排,两边相同中间变1,两边不同中间变0。问N次后的数组样子。

思路1:使用字典记录每一个过程和遍历时的N,如果有重复直接取模。减少运算量。

  1. class Solution {
  2. public:
  3. vector<int> prisonAfterNDays(vector<int>& cells, int N) {
  4. unordered_map<string, int> map;
  5. string firstcell = "";
  6. for (int i = ; i<cells.size(); i++) {
  7. firstcell += to_string(cells[i]);
  8. }
  9. while (N != ) {
  10. if (map.count(firstcell))
  11. N %= map[firstcell] - N;
  12. if(N == ) break;
  13. string nextstr = "";
  14. for (int i = ; i < ; i++) {
  15. nextstr += firstcell[i - ] == firstcell[i + ] ? "" : "";
  16. }
  17. nextstr = "" + nextstr + "";
  18. //cout << nextstr << endl;
  19. map[firstcell] = N;
  20. firstcell = nextstr;
  21. N--;
  22. }
  23. vector<int> ret;
  24. for (int i = ; i<firstcell.size(); i++) {
  25. if (firstcell[i] == '') ret.push_back();
  26. else ret.push_back();
  27. }
  28. return ret;
  29. }
  30. };

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. 5.安装bacula-web(监控页面)

    1.   安装bacula-web(监控页面) 用途:监控bacula状态. http://docs.bacula-web.org/en/master/index.html bacula-web-7. ...

  2. JavaScript 转换数字为整数的方法

    本文将会列举并说明JavaScript 把一个number(或者numerical的对象)转换成一个整数相关方法. 使用parseInt parseInt的语法如下:parseInt(string, ...

  3. CSS基础学习 16.CSS过渡

  4. CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59

    系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...

  5. 【Android-SwipeRefreshLayout控件】下拉刷新

    Android自带API ,V4包下面的下拉刷新控件 android.support.v4.widget.SwipeRefreshLayout SwipeRefreshLayout只能包含一个控件 布 ...

  6. ttf-mscorefonts-installer 无法安装,解决办法

    ttf-mscorefonts-installer 无法安装,解决办法 原 lieefu 发布于 2017/01/11 08:11 字数 163 阅读 1007 收藏 0 点赞 0 评论 0 面试:你 ...

  7. Microsoft.Practices.Unity使用配置文件总是报错The type name or alias could not be resolved.

    Type name could not be resolved. Please check config file http://stackoverflow.com/questions/1493564 ...

  8. vue-cli 3.x 修改dist路径和在本地查看方法

    打包文件路径问题 需要在项目的根目录添加一个vue.config.js.在这个文件中,我们可以进行一些个性化定制. module.exports = { // 基本路径 baseUrl: './', ...

  9. 利用简单的有限状态机(FSM)来实现一个简单的LED流水灯

    有限状态机,(英语:Finite-state machine, FSM),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型. 有限状态机是指输出取决于过去 ...

  10. Django-模板继承/组件/自定义标签

    一.标签tags for标签 遍历每一个元素:  写个for,然后 tab键自动生成for循环的结构,循环很基础,就这么简单的用,没有什么break之类的,复杂一些的功能,你要通过js def get ...