1. 程式人生 > >[LeetCode] 334. Increasing Triplet Subsequence

[LeetCode] 334. Increasing Triplet Subsequence

題目

According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population…
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

Example:

Input: 
[
  [0,1,0],
  [0,0,1],
  [1,1,1],
  [0,0,0]
]
Output: 
[
  [0,0,0],
  [1,0,1],
  [0,1,1],
  [0,1,0]
]

Follow up:

  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

思路

題目大意

一個"0","1"組成的矩陣,其中 "0"代表dead , "1"代表 alive 。進行一次迭代。

  1. 若一個matrix[i][j]為alive (“1”),若他接觸alive元素 大於等於2 且 小於等於3。那麼他的下一代為alive(“1”),否則下一代為 dead(“0”)。
  2. 若一個matrix[i][j]為dead(“0”),若他接觸alive元素 等於3,那麼他的下一代為alive(“1”),否則下一代為dead(“0”)。

要求:計算過程要求 space O(1)

解題思路

題目最大的難點是空間複雜度為 O(1)。 我們改變原矩陣上board的數值,board 由二進位制兩位陣列成。 [1][0] ,[0] 若為1表示當前為 “1”,否則為"0"。[1] 若為1表示下一代為 “1”,否則為"0"。

獲得 當前是否為1 ,x&1 ==1 獲得 下一代是否為1 , x&2 == 2 將下一代置為 1 ,x = x|2

code

class Solution:
    def classify(self, board, n, m, posr, posc):
        neighbors_alives = 0
        for i in range(-1, 2):
            for j in range(-1, 2):
                if i == 0 and j == 0:
                    continue
                elif posr + i >= 0 and posr + i < n and posc + j >= 0 and posc + j < m and board[posr+i][posc+j] & 1:
                    neighbors_alives += 1
        return neighbors_alives

    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        n = len(board)
        m = len(board[0])
        for i in range(n):
            for j in range(m):
                stat = self.classify(board, n, m, i, j)
                if board[i][j] & 1:
                    if 2 <= stat <= 3:
                        board[i][j] = board[i][j] | 2
                else:
                    if stat == 3:
                        board[i][j] = board[i][j] | 2
        for i in range(n):
            for j in range(m):
                if board[i][j] & 2:
                    board[i][j] = 1
                else:
                    board[i][j] = 0