Skip to content

Commit 3297d27

Browse files
author
Partho Biswas
committed
289_Game_of_Life
1 parent 2c1d4d0 commit 3297d27

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ I have solved quite a number of problems from several topics. See the below tabl
154154
|46| **[359. Logger Rate Limiter](https://tinyurl.com/tcr34w3)** | [Python](https://tinyurl.com/wu6rdaw/359_Logger_Rate_Limiter.py)| --- | Easy | Hash and Set, Very important |
155155
|47| **[48. Rotate Image](https://tinyurl.com/yy3z9hab)** | [Python](https://tinyurl.com/wu6rdaw/48_Rotate_Image.py)| [Must](https://tinyurl.com/tr62top), [Vid 1](https://tinyurl.com/uhnruzt) | Medium | Very important |
156156
|48| **[362. Design Hit Counter](https://tinyurl.com/sswrcc3)** | [Python](https://tinyurl.com/wu6rdaw/362_Design_Hit_Counter.py), [Swift](https://tinyurl.com/wuja3c4/362_Design_Hit_Counter.swift)| --- | Medium | --- |
157+
|49| **[289. Game of Life](https://tinyurl.com/y5ujyvu5)** | [Python](https://tinyurl.com/wu6rdaw/289_Game_of_Life.py), [Swift](https://tinyurl.com/wuja3c4/289_Game_of_Life.swift)| --- | Medium | --- |
157158

158159

159160
</p>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# O(mn) approach
2+
class Solution(object):
3+
def gameOfLife(self, board):
4+
"""
5+
:type board: List[List[int]]
6+
:rtype: None Do not return anything, modify board in-place instead.
7+
"""
8+
neighbours = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)]
9+
rows = len(board)
10+
cols = len(board[0])
11+
boardClone = [[board[row][col] for col in range(cols)] for row in range(rows)]
12+
for row in range(rows):
13+
for col in range(cols):
14+
livingNeighbours = 0
15+
for neighbour in neighbours:
16+
r = row + neighbour[0]
17+
c = col + neighbour[1]
18+
if (r < rows and r >= 0) and (c < cols and c >= 0) and (boardClone[r][c] == 1):
19+
livingNeighbours += 1
20+
if boardClone[row][col] == 1 and (livingNeighbours < 2 or livingNeighbours > 3):
21+
board[row][col] = 0
22+
if boardClone[row][col] == 0 and (livingNeighbours == 3):
23+
board[row][col] = 1
24+
25+
26+
# O(1) approach
27+
class Solution(object):
28+
def gameOfLife(self, board):
29+
"""
30+
:type board: List[List[int]]
31+
:rtype: None Do not return anything, modify board in-place instead.
32+
"""
33+
neighbours = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)]
34+
rows = len(board)
35+
cols = len(board[0])
36+
for row in range(rows):
37+
for col in range(cols):
38+
livingNeighbours = 0
39+
for neighbour in neighbours:
40+
r = row + neighbour[0]
41+
c = col + neighbour[1]
42+
if (r < rows and r >= 0) and (c < cols and c >= 0) and (abs(board[r][c]) == 1):
43+
livingNeighbours += 1
44+
if board[row][col] == 1 and (livingNeighbours < 2 or livingNeighbours > 3):
45+
board[row][col] = -1
46+
if board[row][col] == 0 and (livingNeighbours == 3):
47+
board[row][col] = 2
48+
for row in range(rows):
49+
for col in range(cols):
50+
if board[row][col] > 0:
51+
board[row][col] = 1
52+
else:
53+
board[row][col] = 0
54+
55+
56+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Foundation
2+
3+
class Solution {
4+
func gameOfLife(_ board: inout [[Int]]) {
5+
guard board.count > 0 else {
6+
return
7+
}
8+
guard board[0].count > 0 else {
9+
return
10+
}
11+
let rows = board.count
12+
let cols = board[0].count
13+
for row in 0..<rows {
14+
for col in 0..<cols {
15+
let livingNeighbours = self.getLivesCount(row, col, board)
16+
if (board[row][col] == 1) && (livingNeighbours < 2 || livingNeighbours > 3) {
17+
board[row][col] = -1
18+
}
19+
if board[row][col] == 0 && livingNeighbours == 3 {
20+
board[row][col] = 2
21+
}
22+
}
23+
}
24+
for row in 0..<rows {
25+
for col in 0..<cols {
26+
if board[row][col] > 0 {
27+
board[row][col] = 1
28+
} else {
29+
board[row][col] = 0
30+
}
31+
}
32+
}
33+
}
34+
35+
private func getLivesCount(_ row: Int, _ col: Int, _ board: [[Int]]) -> Int {
36+
let rows = board.count
37+
let cols = board[0].count
38+
var neighbours = [(1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1), (1,1)]
39+
var livingNeighbours = 0
40+
for neighbour in neighbours {
41+
let r = row + neighbour.0
42+
let c = col + neighbour.1
43+
if (r < rows && r >= 0) && (c < cols && c >= 0) && (abs(board[r][c]) == 1) {
44+
livingNeighbours += 1
45+
}
46+
}
47+
return livingNeighbours
48+
}
49+
}
50+
51+

0 commit comments

Comments
 (0)