代码改变世界

[LeetCode] 957. Prison Cells After N Days_Medium Tag: Array

2021-07-29 10:54  Johnson_强生仔仔  阅读(29)  评论(0编辑  收藏  举报

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.

You are given an integer array cells where cells[i] == 1 if the ith cell is occupied and cells[i] == 0 if the ith cell is vacant, and you are given an integer n.

Return the state of the prison after n days (i.e., n such changes described above).

 

Example 1:

Input: cells = [0,1,0,1,1,0,0,1], n = 7
Output: [0,0,1,1,0,0,0,0]
Explanation: The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]

Example 2:

Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000
Output: [0,0,1,1,1,1,1,0]

 

Constraints:

  • cells.length == 8
  • cells[i] is either 0 or 1.
  • 1 <= n <= 109

Code:

1. brute force: Time limit out   T: O(n * len(cells))       S: O(len(cells))

class Solution:
    def prisonAfterNDays(self, cells: List[int], n: int) -> List[int]:
        for _ in range(n):
           cells = self.nextDay(cells)
        return cells
    
    
    
    def nextDay(self, cells):
        cells_length = len(cells)
        newCells = [0] * cells_length
        for i in range(1, cells_length - 1):
            newCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
        return newCells
        
        

 

2. maybe 有loop,那么可以用visited 利用空间来换时间, 还是Time limit out: O:T(n)    S: O(n)

Code

class Solution:
    def prisonAfterNDays(self, cells: List[int], n: int) -> List[int]:
        cell_length = len(cells)
        visited = dict()
        for _ in range(n):
            cells = self.nextDay(cells, visited)
        return cells
    
    
    
    def nextDay(self, cells, visited):
        cells_length = len(cells)
        if tuple(cells) not in visited:
            newCells = [0] * cells_length
            for i in range(1, cells_length - 1):
                newCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
            visited[tuple(cells)] = newCells
        return visited[tuple(cells)]

 

3. 想法就是如果有loop,那么我们可以找到steps_loop, 然后用 n %= steps_loop, 来加快进程,如果有loop的话, 用visited = {tuple(cells) : n}, 不停 n-= 1, 一旦tuple(cells) in visited, 

那么我们就知道有loop,并且steps_loop = visited[tuple(cells)] - n. 

class Solution:
    def prisonAfterNDays(self, cells: List[int], n: int) -> List[int]:
        cell_length = len(cells)
        visited, hasCircle = dict(), False
        while n > 0:
            if not hasCircle:
                state_key = tuple(cells)
                if state_key in visited:
                    steps_circle= visited[state_key] - n
                    n %= steps_circle
                    hasCircle = True
                else:
                    visited[state_key] = n
            if n > 0:
                cells = self.nextDay(cells)
                n -= 1
        return cells
    
    
    
    def nextDay(self, cells):
        cells_length = len(cells)
        newCells = [0] * cells_length
        for i in range(1, cells_length - 1):
            newCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
        return newCells