# Prison Cells After N Days
Classic test for work with bits in a byte.
Following description is for a task on leetcode.com.
Description on an Amazon online assessment website is different.
The difference described below.
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
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]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
Note:
cells.length == 8
cells[i] is in {0, 1}
1 <= N <= 10^9
```
The idea of the solution is based of the fact that there are only 2^6 possible combinations of cells because from the second step first
and last cells are always = 0. It means that after 2^6 steps all combinations will repeat again.
So we will keep all combinations of the cells in a hash map until we meet the loop. After it happens we will not calculate the next
combinations but will take precalculated ones from the hash map.
Discussion and test environment are here:
https://leetcode.com/problems/prison-cells-after-n-days/
The difference on an Amazon online assessment website.
1. The first and the last cells in the row can't have two adjacent neighbors so we should consider
these cells as the cells that always have vacant neighbors. In other words an imaginary
cells before the first cell and after the last cells are always empty.
2. If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes vacant.
So the first and the last cells are always occupied.
没有合适的资源?快使用搜索试试~ 我知道了~
Amazon-Previous-OA-questions:此存储库包含所有上一年亚马逊在线评估任务
共149个文件
license:41个
md:41个
gitignore:41个
需积分: 20 1 下载量 21 浏览量
2021-05-30
16:04:35
上传
评论
收藏 341KB ZIP 举报
温馨提示
亚马逊 已知亚马逊在线评估任务的解决方案
资源详情
资源评论
资源推荐
收起资源包目录
Amazon-Previous-OA-questions:此存储库包含所有上一年亚马逊在线评估任务 (149个子文件)
main.cpp 4KB
main.cpp 4KB
main.cpp 4KB
main.cpp 3KB
main.cpp 2KB
main.cpp 2KB
main.cpp 2KB
main.cpp 1KB
main.cpp 1KB
main.cpp 1KB
main.cpp 97B
main.cpp 97B
main.cpp 97B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
.gitignore 382B
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
LICENSE 18KB
README.md 2KB
README.md 2KB
README.md 1KB
README.md 1KB
README.md 1KB
共 149 条
- 1
- 2
大白兔奶棠
- 粉丝: 26
- 资源: 4660
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于pytorch+OpenCV的手写数字识别源码+使用文档+全部资料(优秀项目).zip
- 基于C++和Opencv的传统手势识别源码+使用文档+全部资料(优秀项目).zip
- 与我最爱的人度过的第二个情人节,花心思制作的一个网页送给她
- Python实战:高效读取Excel数据.zip
- 历史学习网站 JAVA+Vue.js+SpringBoot+MySQL
- µ×ɼµÂ͵ƻºÍÒ¿ÏÍɼÂÎ×Á
- 基于pytorch+OpenCV的手写数字识别源码+使用文档+全部资料(优秀项目).zip
- C++ 一个 回文素数 回文素 数
- C++ 一个 回文素数 回文素 数
- 基于pytorch+OpenCV的手写数字识别源码+使用文档+全部资料(优秀项目).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0