-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
28 lines (23 loc) · 871 Bytes
/
solution.py
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
from collections import deque
class Solution:
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
m, n = len(isWater), len(isWater[0])
height = [[-1] * n for _ in range(m)]
queue = deque()
# Initialize water cells
for i in range(m):
for j in range(n):
if isWater[i][j] == 1:
height[i][j] = 0
queue.append((i, j))
# Directions for BFS
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
# BFS
while queue:
x, y = queue.popleft()
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n and height[nx][ny] == -1:
height[nx][ny] = height[x][y] + 1
queue.append((nx, ny))
return height