LeetCode 957 Prison Cells After N Days (Python)

Posted by 小明MaxMing on July 3, 2020

题目

There are 8 prison cells in a row, and each cell is either occupied or vacant.

Each day, whether the cell is occupied or vacant changes according to the following rules:

  • If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
  • Otherwise, it becomes vacant. (Note that because the prison is a row, the first and the last cells in the row can’t have two adjacent neighbors.)

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.

Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)

解题思路

先找到状态的循环,直接找到最后的状态

代码

class Solution:
    def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
        dic = {}
        while N > 0:
            dic[''.join(map(str, cells))] = N
            N -= 1
            tmp = [0] * 8
            for i in range(1, 7):
                tmp[i] = 1 if cells[i - 1] == cells[i + 1] else 0
            cells = tmp
            t = ''.join(map(str, cells))
            if t in dic:
                N = N % (dic[t] - N)
        return cells
class Solution:
    def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
        def find(cells):
            tmp = [0] * 8
            for i in range(1, 7):
                tmp[i] = 1 if cells[i - 1] == cells[i + 1] else 0
            return tmp
        
        cycle = []
        state = find(cells)
        while state not in cycle:
            cycle.append(state)
            state = find(state)
        return cycle[(N - 1) % len(cycle)]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅