Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 289 · Matrix

Game of Life

LeetCode 289 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

In-place state encoding: −1 (dies) and 2 (born).

நடுத்தரம்ArrayMatrixSimulation
LeetCode இல் திற ↗

நான் Game of Life (LeetCode 289) ஐ இப்படித் தீர்த்தேன் — Matrix பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Every cell must update from the old board, so you can't overwrite values as you go. Encode transitions in place: −1 means 'was alive, now dead', 2 means 'was dead, now alive'. Counting neighbours with abs(value) == 1 still sees the old state.

Approach

  1. 1

    Count live neighbours using abs(cell) == 1.

  2. 2

    A live cell with < 2 or > 3 neighbours becomes −1.

  3. 3

    A dead cell with exactly 3 neighbours becomes 2.

  4. 4

    Final pass: positive → 1, negative → 0.

நான் எப்படித் தீர்த்தேன்

In-place state encodingPythonO(m·n) time · O(1) space
class Solution:
    def gameOfLife(self, board: List[List[int]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        #live_neighbours<2 - dies
        #live_neighbours>3 - dies
        #live_neighbours==3
        m = len(board)
        n = len(board[0])
        arr_copy=board.copy()
        for i in range(m):
            for j in range(n):
                live_neighbours=0
                for di in range(-1,2):
                    for dj in range(-1,2):
                        if not (di == 0 and dj == 0):
                            nrow = i+di
                            ncol = j+dj
                            if 0<=nrow<m and 0<=ncol<n and abs(board[nrow][ncol])==1:
                                live_neighbours+=1
                if (live_neighbours<2 or live_neighbours>3) and board[i][j]==1:
                    board[i][j]=-1
                if live_neighbours==3 and board[i][j]==0:
                    board[i][j]=2
        for i in range(m):
            for j in range(n):
                if board[i][j]>0:
                    board[i][j]=1
                elif board[i][j]<0:
                    board[i][j]=0

ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Game of Life — அடிக்கடி கேட்கப்படும் கேள்விகள்

Game of Life பின்னாலுள்ள intuition என்ன?

Every cell must update from the old board, so you can't overwrite values as you go. Encode transitions in place: −1 means 'was alive, now dead', 2 means 'was dead, now alive'. Counting neighbours with abs(value) == 1 still sees the old state.

Game of Life (LeetCode 289) ஐ எப்படித் தீர்ப்பது?

1. Count live neighbours using abs(cell) == 1. 2. A live cell with < 2 or > 3 neighbours becomes −1. 3. A dead cell with exactly 3 neighbours becomes 2. 4. Final pass: positive → 1, negative → 0.

Game of Life இன் time மற்றும் space complexity என்ன?

Time: O(m·n). Space: O(1). (Accepted approach: In-place state encoding.)

Game of Life இல் பொதுவான தவறுகள் என்ன?

board.copy() is shallow — it doesn't protect the rows; the encoding is what makes it correct.