일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- shiba
- 43. Multiply Strings
- 30. Substring with Concatenation of All Words
- DWG
- Decorator
- 컴퓨터의 구조
- 시바견
- Substring with Concatenation of All Words
- LeetCode
- Python
- 밴픽
- Python Code
- kaggle
- data science
- Regular Expression
- Convert Sorted List to Binary Search Tree
- t1
- Generator
- 715. Range Module
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- 운영체제
- concurrency
- attribute
- 315. Count of Smaller Numbers After Self
- 파이썬
- Protocol
- Class
- iterator
Archives
- Today
- Total
Scribbling
LeetCode: 130. Surrounded Regions 본문
고립된 O를 X로 만드는 문제이다.
"고립된"은 board의 경계와 이어지지 않는 O들이다.
고로 경계의 O와 이어진 O들을 제외하고, 모두 X 처리해준다.
from collections import deque
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
island = set()
m, n = len(board), len(board[0])
for i in range(m):
for j in range(n):
if board[i][j] == "O":
island.add((i, j))
visited = set()
for i in range(m):
for j in range(n):
# edge
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
if board[i][j] == "O":
# if not visited
if (i, j) not in visited:
# bfs to find non_island
q = deque()
q.append((i, j))
visited.add((i, j))
while q:
now = q.popleft()
island.discard(now)
for d in range(4):
next_y = now[0] + dy[d]
next_x = now[1] + dx[d]
if 0 <= next_y <= m - 1 and 0 <= next_x <= n - 1:
if (next_y, next_x) not in visited and \
board[next_y][next_x] == "O":
q.append((next_y, next_x))
visited.add((next_y, next_x))
for coord in island:
board[coord[0]][coord[1]] = "X"
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 131. Palindrome Partitioning (0) | 2021.11.13 |
---|---|
LeetCode: 146. LRU Cache (0) | 2021.11.11 |
LeetCode: 129. Sum Root to Leaf Numbers (0) | 2021.11.10 |
프로그래머스: 오픈채팅방 (0) | 2021.11.08 |
LeetCode: 128. Longest Consecutive Sequence (0) | 2021.11.08 |