-
Notifications
You must be signed in to change notification settings - Fork 43
/
prison-cells-after-n-days.py
66 lines (63 loc) · 1.74 KB
/
prison-cells-after-n-days.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# V0
# V1
# https://www.twblogs.net/a/5c162c02bd9eee5e40bbaace/zh-cn
# IDEA : DP + MOD
class Solution(object):
def prisonAfterNDays(self, oldcells, N):
"""
:type cells: List[int]
:type N: int
:rtype: List[int]
"""
cells = copy.deepcopy(oldcells)
count = 0
N %= 14
if N == 0:
N = 14
while count < N:
newCell = [0] * 8
for i in range(1, 7):
if cells[i - 1] == cells[i + 1]:
newCell[i] = 1
else:
newCell[i] = 0
cells = newCell
count += 1
return cells
# V2
# Time: O(1)
# Space: O(1)
class Solution(object):
def prisonAfterNDays(self, cells, N):
"""
:type cells: List[int]
:type N: int
:rtype: List[int]
"""
N -= max(N-1, 0) // 14 * 14 # 14 is got from Solution2
for i in range(N):
cells = [0] + [cells[i-1] ^ cells[i+1] ^ 1 for i in range(1, 7)] + [0]
return cells
# Time: O(1)
# Space: O(1)
class Solution2(object):
def prisonAfterNDays(self, cells, N):
"""
:type cells: List[int]
:type N: int
:rtype: List[int]
"""
cells = tuple(cells)
lookup = {}
while N:
lookup[cells] = N
N -= 1
cells = tuple([0] + [cells[i - 1] ^ cells[i + 1] ^ 1 for i in range(1, 7)] + [0])
if cells in lookup:
assert(lookup[cells] - N in (1, 7, 14))
N %= lookup[cells] - N
break
while N:
N -= 1
cells = tuple([0] + [cells[i - 1] ^ cells[i + 1] ^ 1 for i in range(1, 7)] + [0])
return list(cells)