1
# Runtime: 85 ms (Top 14.83%) | Memory: 14 MB (Top 28.83%)3
def prisonAfterNDays(self, cells: List[int], n: int) -> List[int]:4
patternMatch = defaultdict(int) # pattern match5
totalPrisons = 8 # totalPrisons6
cells = [str(c) for c in (cells)] # into char type7
for d in range(1, n + 1):9
tempCell.append("0") # left corner case10
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"14
tempCell.append("1") # insert 1 if first condition met16
tempCell.append("0") # otherwise 017
tempCell.append("0") # right corner case18
cells = tempCell # update cells19
pattern = "".join(tempCell) # insert pattern in hashtable20
if pattern in patternMatch: # if there is a match21
day = patternMatch[pattern]22
remainder = (n % (d - 1)) - 1 # take modulo23
match = list(patternMatch.keys())[remainder] # find key24
return [int(m) for m in match] # return25
patternMatch[pattern] = d # assign day26
return [int(c) for c in (cells)] # return