[
graph
bfs
2d-array
]
BinarySearch 0783 Peak Heights
Problem statement
https://binarysearch.com/problems/Peak-Heights/
Solution
Equal to Leetcode 1765. Map of Highest Peak.
Complexity
It is O(mn)
for time and space.
Code
class Solution:
def solve(self, grid):
m, n = len(grid), len(grid[0])
q = deque([(0, i, j) for i in range(m) for j in range(n) if grid[i][j] == 0])
ans = 0
V = set()
while q:
dist, i, j = q.popleft()
for x, y in (1, 0), (-1, 0), (0, 1), (0, -1):
xi, yj = x+i, y+j
if 0 <= xi < m and 0 <= yj < n and grid[xi][yj] == 1 and (xi, yj) not in V:
V.add((xi, yj))
q += [(dist + 1, xi, yj)]
grid[xi][yj] = dist + 1
return grid