Cell Colony Next Generation
A colony lives on a grid where 1 is a live cell and 0 a dead one. Each cell has up to eight neighbours (including diagonals); cells outside the grid count as dead. All cells update at the same time:
- a live cell with 2 or 3 live neighbours stays alive, otherwise it dies; - a dead cell with exactly 3 live neighbours becomes alive.
Return the grid after one generation.
Example 1
Input: board = [[0,0,0],[1,1,1],[0,0,0]] Output: [[0,1,0],[0,1,0],[0,1,0]]
Example 2
Input: board = [[1,1],[1,0]] Output: [[1,1],[1,1]]
Constraints
- 1 <= rows, cols <= 100
- board[r][c] is 0 or 1
Hint 1
Build a new grid so that updates do not affect neighbour counts of cells you have not processed yet.
Hint 2
Count the eight neighbours with bounds checks.
Discussion 0
Join free to post in the discussion.
Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Game Developer
Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.