Skip to content

Commit 7d4c6c5

Browse files
committed
added Prison Cells After N Days medium
1 parent 5461093 commit 7d4c6c5

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Prison Cells After N Days
3+
[Leetcode Link](https://leetcode.com/problems/prison-cells-after-n-days/)
4+
5+
## Problem:
6+
7+
There are 8 prison cells in a row, and each cell is either occupied or vacant.
8+
9+
Each day, whether the cell is occupied or vacant changes according to the following rules:
10+
11+
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
12+
Otherwise, it becomes vacant.
13+
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
14+
15+
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`.
16+
17+
Given the initial state of the prison, return the state of the prison after `N` days (and `N` such changes described above.)
18+
19+
## Example:
20+
21+
```
22+
Input: cells = [0,1,0,1,1,0,0,1], N = 7
23+
Output: [0,0,1,1,0,0,0,0]
24+
Explanation:
25+
The following table summarizes the state of the prison on each day:
26+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
27+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
28+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
29+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
30+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
31+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
32+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
33+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
34+
```
35+
```
36+
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
37+
Output: [0,0,1,1,1,1,1,0]
38+
```
39+
40+
## Note:
41+
42+
1. `cells.length == 8`
43+
2. `cells[i]` is in `{0, 1}`
44+
3. `1 <= N <= 10^9`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
4+
def prisonAfterNDays(cells: List[int], N: int) -> List[int]:
5+
# the pattern repeats every 15 days
6+
N = (N-1) % 14 + 1
7+
prev = cells
8+
current = list()
9+
for day in range(N):
10+
for i in range(len(prev)):
11+
# first and last cell is always vacant because it does not have two adjacent neighbors
12+
if i == 0 or i == len(prev)-1:
13+
current.append(0)
14+
# insert 0 or 1 based on neighboring cells
15+
else:
16+
isOccupied = 1 if not(prev[i-1] ^ prev[i+1]) else 0
17+
current.append(isOccupied)
18+
# after each day, set current to be prev, and new list as current
19+
prev = current
20+
# print("Day", day+1, ":", current)
21+
current = list()
22+
return prev
23+
24+
25+
# test driver
26+
cells = [0, 1, 0, 1, 1, 0, 0, 1]
27+
N = 7
28+
print("Input: cells =", cells, ", N =", N)
29+
print("Output:", prisonAfterNDays(cells, N))
30+
print()
31+
32+
cells = [1, 0, 0, 1, 0, 0, 1, 0]
33+
N = 1000000000
34+
print("Input: cells =", cells, ", N =", N)
35+
print("Output:", prisonAfterNDays(cells, N))
36+
print()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Languages used: Java and Python
4545
- [Search in Rotated Sorted Array](Medium/SearchInRotatedSortedArray)
4646
- [Car Pooling](Medium/CarPooling)
4747
- [Palindromic Substrings](Medium/PalindromicSubstrings)
48+
- [Prison Cells After N Days](Medium/PrisonCellsAfterNDays)
4849
- Hard
4950

5051
---

0 commit comments

Comments
 (0)