1
# Runtime: 85 ms (Top 14.83%) | Memory: 14 MB (Top 28.83%)
2
class Solution:
3
def prisonAfterNDays(self, cells: List[int], n: int) -> List[int]:
4
patternMatch = defaultdict(int) # pattern match
5
totalPrisons = 8 # totalPrisons
6
cells = [str(c) for c in (cells)] # into char type
7
for d in range(1, n + 1):
8
tempCell = []
9
tempCell.append("0") # left corner case
10
for c in range(1, totalPrisons - 1):
11
if (cells[c - 1] == "1" and cells[c + 1] == "1") or (
12
cells[c - 1] == "0" and cells[c + 1] == "0"
13
):
14
tempCell.append("1") # insert 1 if first condition met
15
else:
16
tempCell.append("0") # otherwise 0
17
tempCell.append("0") # right corner case
18
cells = tempCell # update cells
19
pattern = "".join(tempCell) # insert pattern in hashtable
20
if pattern in patternMatch: # if there is a match
21
day = patternMatch[pattern]
22
remainder = (n % (d - 1)) - 1 # take modulo
23
match = list(patternMatch.keys())[remainder] # find key
24
return [int(m) for m in match] # return
25
patternMatch[pattern] = d # assign day
26
return [int(c) for c in (cells)] # return

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0