Game of Life - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
1. 구현
from typing import *
'''
Any live cell with fewer than two live neighbors dies as if caused by under-population.
8방으로 이웃이 2개 미만이면 죽인다
Any live cell with two or three live neighbors lives on to the next generation.
8방으로 이웃이 2개 혹은 3개면 살린다
Any live cell with more than three live neighbors dies, as if by over-population.
8방으로 이웃이 4개 이상이면 죽인다
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
8방으로 이웃이 정확히 3개면 생산한다.
'''
class Solution:
def neighbor_num(self, x, y, board):
xpo = [0, 0, 1, -1, 1, -1, 1, -1]
ypo = [1, -1, 0, 0, 1, 1, -1, -1]
answer = 0
for i in range(8):
new_x, new_y = x + xpo[i], y + ypo[i]
if 0 <= new_x < len(board) and 0 <= new_y < len(board[0]):
if board[new_x][new_y] == 1:
answer += 1
return answer
def gameOfLife(self, board: List[List[int]]) -> None:
born = []
die = []
for i in range(len(board)):
for j in range(len(board[i])):
temp = self.neighbor_num(i, j, board)
if temp == 3:
born.append((i, j))
elif temp < 2:
die.append((i, j))
elif temp >= 4:
die.append((i, j))
for item in born:
x, y = item
board[x][y] = 1
for item in die:
x, y = item
board[x][y] = 0
좌표에서 8방을 보고 1의 개수 조건에 맞게 처리를 하는 구현 문제였다.
포인트는 실시간으로 바꿔주는 것이 아니라 0으로 만들 좌표, 1로 만들 좌표를 미리 저장 후에 마지막에 반영해주는 것.
'Algorithm' 카테고리의 다른 글
122. Best Time to Buy and Sell Stock II (0) | 2021.05.27 |
---|---|
378. Kth Smallest Element in a Sorted Matrix (0) | 2021.05.25 |
191. Number of 1 Bits (0) | 2021.05.23 |
108. Convert Sorted Array to Binary Search Tree (0) | 2021.05.23 |
300. Longest Increasing Subsequence (0) | 2021.05.19 |