일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 파이썬
- 시바견
- 715. Range Module
- t1
- 밴픽
- attribute
- Python
- 30. Substring with Concatenation of All Words
- 컴퓨터의 구조
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- 43. Multiply Strings
- Generator
- Class
- Substring with Concatenation of All Words
- concurrency
- iterator
- 109. Convert Sorted List to Binary Search Tree
- DWG
- 운영체제
- Regular Expression
- data science
- LeetCode
- Decorator
- kaggle
- 315. Count of Smaller Numbers After Self
- Python Implementation
- Python Code
- Protocol
- shiba
Archives
- Today
- Total
Scribbling
LeetCode: 407. Trapping Rain Water II 본문
class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
import heapq
boundaries = []
m, n = len(heightMap), len(heightMap[0])
visited = [[False] * n for _ in range(m)]
for y in range(m):
for x in range(n):
if y == 0 or y == m - 1 or x == 0 or x == n - 1:
heapq.heappush(boundaries, (heightMap[y][x], y, x))
visited[y][x] = True
ret = 0
dy, dx = [0, 1, 0, -1], [1, 0, -1, 0]
while boundaries:
h, y, x = heapq.heappop(boundaries)
for d in range(4):
next_y, next_x = y + dy[d], x + dx[d]
if 0 <= next_y < m and 0 <= next_x < n and not visited[next_y][next_x]:
if heightMap[next_y][next_x] >= h:
heapq.heappush(boundaries, (heightMap[next_y][next_x], next_y, next_x))
else:
ret += h - heightMap[next_y][next_x]
heapq.heappush(boundaries, (h, next_y, next_x))
visited[next_y][next_x] = True
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 745. Prefix and Suffix Search (0) | 2022.03.10 |
---|---|
LeetCode: 1834. Single-Threaded CPU (0) | 2022.03.08 |
LeetCode: 1610. Maximum Number of Visible Points (0) | 2022.03.07 |
LeetCode: 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (0) | 2022.03.04 |
LeetCode: 715. Range Module (0) | 2022.03.03 |