일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- Decorator
- 시바견
- 운영체제
- Substring with Concatenation of All Words
- Protocol
- 315. Count of Smaller Numbers After Self
- LeetCode
- 컴퓨터의 구조
- kaggle
- Convert Sorted List to Binary Search Tree
- t1
- Class
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- DWG
- iterator
- 파이썬
- Regular Expression
- attribute
- concurrency
- shiba
- data science
- 30. Substring with Concatenation of All Words
- Python Code
- 715. Range Module
- Python
- Generator
- 밴픽
- 43. Multiply Strings
Archives
- Today
- Total
Scribbling
LeetCode: 289. Game of Life 본문
To solve this problem in-place, we can think of using two more states(2, 3) rather than two states(1, 0).
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
for y in range(m):
for x in range(n):
cnt = self.countOnes(board, y, x)
if board[y][x] == 0:
if cnt == 3:
board[y][x] = 3
else:
if cnt != 2 and cnt != 3:
board[y][x] = 2
for y in range(m):
for x in range(n):
if board[y][x] == 3:
board[y][x] = 1
elif board[y][x] == 2:
board[y][x] = 0
def countOnes(self, board, y, x):
dy = [-1, -1, 0, 1, 1, 1, 0, -1]
dx = [0, 1, 1, 1, 0, -1, -1, -1]
cnt = 0
for d in range(8):
next_y, next_x = y + dy[d], x + dx[d]
if 0 <= next_y <= len(board) - 1 and 0 <= next_x <= len(board[0]) - 1:
if board[next_y][next_x] == 1 or board[next_y][next_x] == 2:
cnt += 1
return cnt
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 297. Serialize and Deserialize Binary Tree (0) | 2022.01.02 |
---|---|
LeetCode: 328. Odd Even Linked List (0) | 2021.12.31 |
LeetCode: 287. Find the Duplicate Number (0) | 2021.12.28 |
LeetCode: 279. Perfect Squares (0) | 2021.12.27 |
LeetCode: 240. Search a 2D Matrix II (0) | 2021.12.27 |