-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-of-life.js
More file actions
217 lines (186 loc) · 6.09 KB
/
game-of-life.js
File metadata and controls
217 lines (186 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Problem: Game of Life
* Link: https://leetcode.com/problems/game-of-life/
* Difficulty: Medium
*
* The board is made up of an m x n grid of cells, where each cell has an initial state:
* live (1) or dead (0). Each cell interacts with its eight neighbors using the following rules:
* 1. Any live cell with fewer than two live neighbors dies (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 (over-population).
* 4. Any dead cell with exactly three live neighbors becomes a live cell (reproduction).
*
* Update the board in-place to represent the next state.
*
* Time Complexity: O(m * n)
* Space Complexity: O(1)
*/
// JavaScript Solution - In-place with state encoding
function gameOfLife(board) {
if (!board || board.length === 0) return;
const m = board.length;
const n = board[0].length;
// Directions for 8 neighbors: up, down, left, right, and 4 diagonals
const directions = [
[-1, -1], [-1, 0], [-1, 1],
[0, -1], [0, 1],
[1, -1], [1, 0], [1, 1]
];
// Count live neighbors for a cell
function countLiveNeighbors(row, col) {
let count = 0;
for (const [dx, dy] of directions) {
const newRow = row + dx;
const newCol = col + dy;
// Check bounds and count live cells (1 or 2 means currently/previously alive)
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n) {
if (board[newRow][newCol] === 1 || board[newRow][newCol] === 2) {
count++;
}
}
}
return count;
}
// First pass: mark cells with intermediate states
// 0: dead -> dead
// 1: live -> live
// 2: live -> dead
// 3: dead -> live
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const liveNeighbors = countLiveNeighbors(i, j);
if (board[i][j] === 1) {
// Live cell
if (liveNeighbors < 2 || liveNeighbors > 3) {
board[i][j] = 2; // Will die
}
} else {
// Dead cell
if (liveNeighbors === 3) {
board[i][j] = 3; // Will become alive
}
}
}
}
// Second pass: update to final states
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === 2) {
board[i][j] = 0; // Dies
} else if (board[i][j] === 3) {
board[i][j] = 1; // Becomes alive
}
}
}
}
// Alternative solution with extra space O(m*n)
function gameOfLifeWithCopy(board) {
if (!board || board.length === 0) return;
const m = board.length;
const n = board[0].length;
// Create a copy of the board
const copy = board.map(row => [...row]);
const directions = [
[-1, -1], [-1, 0], [-1, 1],
[0, -1], [0, 1],
[1, -1], [1, 0], [1, 1]
];
function countLiveNeighbors(row, col) {
let count = 0;
for (const [dx, dy] of directions) {
const newRow = row + dx;
const newCol = col + dy;
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n) {
if (copy[newRow][newCol] === 1) {
count++;
}
}
}
return count;
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const liveNeighbors = countLiveNeighbors(i, j);
if (copy[i][j] === 1) {
// Live cell
board[i][j] = (liveNeighbors === 2 || liveNeighbors === 3) ? 1 : 0;
} else {
// Dead cell
board[i][j] = (liveNeighbors === 3) ? 1 : 0;
}
}
}
}
// Test case
const board = [
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
];
gameOfLife(board);
console.log(board);
// Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
module.exports = gameOfLife;
/* Python Solution (commented):
def game_of_life(board: list[list[int]]) -> None:
"""
Update the board in-place to represent the next state of Conway's Game of Life.
Args:
board: m x n grid representing the current state
Time Complexity: O(m * n)
Space Complexity: O(1)
"""
if not board:
return
m, n = len(board), len(board[0])
# Directions for 8 neighbors
directions = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
def count_live_neighbors(row: int, col: int) -> int:
"""Count live neighbors for a cell"""
count = 0
for dx, dy in directions:
new_row, new_col = row + dx, col + dy
# Check bounds and count live cells
if 0 <= new_row < m and 0 <= new_col < n:
if board[new_row][new_col] in [1, 2]:
count += 1
return count
# First pass: mark cells with intermediate states
# 0: dead -> dead
# 1: live -> live
# 2: live -> dead
# 3: dead -> live
for i in range(m):
for j in range(n):
live_neighbors = count_live_neighbors(i, j)
if board[i][j] == 1:
# Live cell
if live_neighbors < 2 or live_neighbors > 3:
board[i][j] = 2 # Will die
else:
# Dead cell
if live_neighbors == 3:
board[i][j] = 3 # Will become alive
# Second pass: update to final states
for i in range(m):
for j in range(n):
if board[i][j] == 2:
board[i][j] = 0 # Dies
elif board[i][j] == 3:
board[i][j] = 1 # Becomes alive
# Test case
board = [
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
game_of_life(board)
print(board)
# Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
*/