Skip to content

Commit 12d7430

Browse files
author
Partho Biswas
committed
957. Prison Cells After N Days
1 parent f3d0bb5 commit 12d7430

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ I have solved quite a number of problems from several topics. See the below tabl
177177
|66| [246. Strobogrammatic Number](https://tinyurl.com/ycqwsozh) | [Python](https://tinyurl.com/wu6rdaw/246_Strobogrammatic_Number.py), [Swift](https://tinyurl.com/wuja3c4/246_Strobogrammatic_Number.swift) | | Easy | |
178178
|67| **[845. Longest Mountain in Array](https://tinyurl.com/y9h5uah5)** | [Python](https://tinyurl.com/wu6rdaw/845_Longest_Mountain_in_Array.py), [Swift](https://tinyurl.com/wuja3c4/845_Longest_Mountain_in_Array.swift) | | Medium | |
179179
|68| **[66. Plus One](https://tinyurl.com/yd67rugq)** | [Python](https://tinyurl.com/wu6rdaw/66_Plus_One.py), [Swift](https://tinyurl.com/wuja3c4/66_Plus_One.swift) | | Easy | |
180+
|69| **[957. Prison Cells After N Days](https://tinyurl.com/yag46zkm)** | [Python](https://tinyurl.com/wu6rdaw/957_Prison_Cells_After_N_Days.py), [Swift](https://tinyurl.com/wuja3c4/957_Prison_Cells_After_N_Days.swift) | [Art 1](https://tinyurl.com/y7cbf32e), [Art 2](https://tinyurl.com/yd2y66dn), [Art 3](https://tinyurl.com/yczdwnwu), [Art 4](https://tinyurl.com/y8j9b593), [Art 5](https://tinyurl.com/y6u2u42m) | Medium | |
180181

181182

182183
</p>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
{
3+
'[0, 1, 0, 0, 0, 1, 0, 0]': 1000000001, ----
4+
'[0, 1, 0, 1, 0, 0, 1, 0]': 999999992,
5+
'[0, 0, 1, 1, 1, 0, 0, 0]': 15,
6+
'[0, 1, 0, 0, 1, 1, 1, 0]': 1000000000,
7+
'[0, 0, 0, 1, 1, 1, 0, 0]': 999999997,
8+
'[0, 1, 0, 0, 1, 0, 1, 0]': 999999999,
9+
'[0, 0, 1, 0, 1, 0, 1, 0]': 999999995,
10+
'[0, 1, 0, 1, 0, 1, 0, 0]': 13,
11+
'[0, 1, 1, 1, 0, 0, 1, 0]': 999999993,
12+
'[0, 0, 1, 0, 0, 0, 1, 0]': 999999994,
13+
'[0, 1, 0, 0, 1, 0, 0, 0]': 999999998,
14+
'[0, 0, 1, 1, 1, 1, 1, 0]': 999999996, ------
15+
'[0, 1, 1, 1, 1, 1, 0, 0]': 14,
16+
'[0, 0, 0, 1, 0, 0, 1, 0]': 999999991}
17+
18+
"""
19+
# My initial code. 248 / 258 test cases passed.
20+
# Forward calculations
21+
class Solution(object):
22+
def prisonAfterNDays(self, cells, N):
23+
"""
24+
:type cells: List[int]
25+
:type N: int
26+
:rtype: List[int]
27+
"""
28+
seen = {}
29+
lastSeenCells = []
30+
currentDay = 0
31+
while currentDay < N:
32+
cells = [0] + [cells[i - 1] ^ cells[i + 1] ^ 1 for i in range(1, 7)] + [0]
33+
key = str(cells)
34+
if key in seen:
35+
div, mod = divmod(N, currentDay)
36+
currentDay = N - mod
37+
currentDay += 1
38+
seen[key] = currentDay
39+
lastSeenCells = cells
40+
print(seen)
41+
return lastSeenCells
42+
43+
def prisonAfterNDays(self, cells, N):
44+
seen = {str(cells): N}
45+
while N:
46+
seen.setdefault(str(cells), N)
47+
N -= 1
48+
cells = [0] + [cells[i - 1] ^ cells[i + 1] ^ 1 for i in range(1, 7)] + [0]
49+
if str(cells) in seen:
50+
N %= seen[str(cells)] - N
51+
return cells
52+
53+
54+
# https://tinyurl.com/yczdwnwu
55+
# Backword calculations
56+
class Solution(object):
57+
def prisonAfterNDays(self, cells, N):
58+
seen = {str(cells): N}
59+
while N:
60+
seen.setdefault(str(cells), N)
61+
N -= 1
62+
cells = [0] + [cells[i - 1] ^ cells[i + 1] ^ 1 for i in range(1, 7)] + [0]
63+
if str(cells) in seen:
64+
N %= seen[str(cells)] - N
65+
return cells

0 commit comments

Comments
 (0)