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 ...
随机推荐
- 根治android studio无法预览xml布局的问题
xml报错“Failed to load AppCompat ActionBar with unknown error.” 修改build-gradle文件中的appcompat包导入的版本,直接用“ ...
- C#验证控件使用方法及常用正则表达式例析(转)
ASP.NET为开发人员提供了一整套完整的服务器控件来验证用户输入的信息是否有效.这些控件如下: 1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交 ...
- BAT脚本批量调用Sql执行文件 (SqlServer 数据库)
@echo off & setlocal EnableDelayedExpansion set num=0set INSTANCE_HOSTNAME= 地址set INSTANCE_PORT= ...
- sql 语句用法
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 ...
- python : import详解。
用户输入input() input()函数: 用于从标准输入读取数值. >>> message = input('tell me :') tell me :hahah >> ...
- 通过LVM备份mysql数据库脚本
#!/bin/bash #******************************************************************** #encoding -*-utf8- ...
- 2.Locust 跑起来试试
代码 from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): @task def baidu(self): ...
- 零基础免费搭建个人博客-hexo+github
使用hexo生成静态博客并架设在免费的github page平台 准备 系统: Window 7 64位 使用软件: Git v1.9.5[下载地址] 百度云 360云盘 访问密码 d269 Git官 ...
- sublimeTest3的安装注册插件
[感谢:https://blog.csdn.net/wxl1555/article/details/69941451 ]1)下载:http://www.sublimetext.com/32)安装:(我 ...
- Bootstrap-轮播图-No.8
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...