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 ...
随机推荐
- 5.安装bacula-web(监控页面)
1. 安装bacula-web(监控页面) 用途:监控bacula状态. http://docs.bacula-web.org/en/master/index.html bacula-web-7. ...
- JavaScript 转换数字为整数的方法
本文将会列举并说明JavaScript 把一个number(或者numerical的对象)转换成一个整数相关方法. 使用parseInt parseInt的语法如下:parseInt(string, ...
- CSS基础学习 16.CSS过渡
- CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59
系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...
- 【Android-SwipeRefreshLayout控件】下拉刷新
Android自带API ,V4包下面的下拉刷新控件 android.support.v4.widget.SwipeRefreshLayout SwipeRefreshLayout只能包含一个控件 布 ...
- ttf-mscorefonts-installer 无法安装,解决办法
ttf-mscorefonts-installer 无法安装,解决办法 原 lieefu 发布于 2017/01/11 08:11 字数 163 阅读 1007 收藏 0 点赞 0 评论 0 面试:你 ...
- 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 ...
- vue-cli 3.x 修改dist路径和在本地查看方法
打包文件路径问题 需要在项目的根目录添加一个vue.config.js.在这个文件中,我们可以进行一些个性化定制. module.exports = { // 基本路径 baseUrl: './', ...
- 利用简单的有限状态机(FSM)来实现一个简单的LED流水灯
有限状态机,(英语:Finite-state machine, FSM),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型. 有限状态机是指输出取决于过去 ...
- Django-模板继承/组件/自定义标签
一.标签tags for标签 遍历每一个元素: 写个for,然后 tab键自动生成for循环的结构,循环很基础,就这么简单的用,没有什么break之类的,复杂一些的功能,你要通过js def get ...