Why it works
- Every cell must be decided from the board as it was. Writing 0 or 1 at once would change the count of the cells after it: (2, 1) would see one alive neighbour instead of three and stay dead.
- The marks keep both states in one cell: 1 or 2 means alive now, 0 or 3 dead now, so a count stays right while the board fills with decisions; 2 or 3 also says the cell changes.
- Once every cell is decided, the old states are no longer needed. The new state is the value mod 2 (0 and 2 are dead next, 1 and 3 alive), so the second pass is one line with no checks.
Complexity
Time O(rows × columns): each cell looks at its at most eight neighbours once, then is decoded once. Memory O(1): no copy of the board, the marks live in the cells.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗