Map of Highest Peak LeetCode solution in Python

1
class Solution:
2
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
3
arr = collections.deque()
4
m, n = len(isWater), len(isWater[0])
5
for i in range(m):
6
for j in range(n):
7
if isWater[i][j] == 1:
8
arr.append((0, i, j))
9

10
ans = [[-1] * n for _ in range(m)]
11
while arr:
12
val, x, y = arr.popleft()
13
if ans[x][y] != -1:
14
continue
15
ans[x][y] = val
16
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
17
xx, yy = x + dx, y + dy
18
if 0 <= xx < m and 0 <= yy < n and ans[xx][yy] == -1:
19
arr.append((val + 1, xx, yy))
20
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0