-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe9dbe7
commit b62dc29
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# [298 - Game of Life](https://leetcode.com/problems/find-k-th-smallest-pair-distance/) | ||
### Difficulty: Medium | ||
|
||
### Problem Statement | ||
|
||
According to [Wikipedia's article](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life): "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." | ||
|
||
The board is made up of an `m` x `n` grid of cells, where each cell has an initial state: `live` (represented by a `1`) or `dead` (represented by a `0`). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): | ||
|
||
1. Any live cell with fewer than two live neighbors dies as if caused by under-population. | ||
2. Any live cell with two or three live neighbors lives on to the next generation. | ||
3. ny live cell with more than three live neighbors dies, as if by over-population. | ||
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. | ||
|
||
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the `m` x `n` grid board, return the next state. | ||
|
||
|
||
Example 1: | ||
|
||
#### Example 1: | ||
``` | ||
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]] | ||
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]] | ||
``` | ||
Then the 1st smallest distance pair is (1,1), and its distance is 0. | ||
#### Example 2: | ||
``` | ||
Input: board = [[1,1],[1,0]] | ||
Output: [[1,1],[1,1]] | ||
``` | ||
|
||
#### Constraints: | ||
``` | ||
m == board.length | ||
n == board[i].length | ||
1 <= m, n <= 25 | ||
board[i][j] is 0 or 1. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import copy | ||
|
||
class Solution: | ||
def gameOfLife(self, board: List[List[int]]) -> None: | ||
""" | ||
Do not return anything, modify board in-place instead. | ||
""" | ||
next_state = copy.deepcopy(board) | ||
for i in range(len(board)): | ||
for j in range(len(board[i])): | ||
aliveNeighbors = self.countAliveNeighbors(board, i, j) | ||
next_state[i][j] = self.nextCellState(aliveNeighbors, board[i][j]) | ||
|
||
# final update of board | ||
for i in range(len(board)): | ||
for j in range(len(board[i])): | ||
board[i][j] = next_state[i][j] | ||
|
||
def countAliveNeighbors(self, board: List[List[int]], m: int, n: int): | ||
count = 0 | ||
for i in range(m-1, m+2): | ||
for j in range(n-1, n+2): | ||
if 0 <= i < len(board) and 0 <= j < len(board[0]) and (i,j) != (m,n): | ||
count += board[i][j] | ||
return count | ||
|
||
def nextCellState(self, count: int, status: int): | ||
#4 | ||
if count == 3 and status == 0: | ||
return 1 | ||
#2 | ||
elif status == 1 and 2 <= count <= 3: | ||
return 1 | ||
# 1 and 3 | ||
return 0 |