Leetcode 1876 Solution
This article provides solution to leetcode question 1876 (map-of-highest-peak)
Access this page by simply typing in "lcs 1876" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/map-of-highest-peak
Solution
class Solution:
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
m = len(isWater)
n = len(isWater[0])
heights = [[-1 for _ in range(n)] for _ in range(m)]
q = collections.deque()
curr_height = 0
# Find out all zeros.
water_pos = []
for i in range(m):
for j in range(n):
if not isWater[i][j]:
continue
heights[i][j] = curr_height
q.append((i, j))
while q:
curr_height += 1
s = len(q)
for _ in range(s):
i, j = q.popleft()
if i > 0 and heights[i - 1][j] == -1:
heights[i - 1][j] = curr_height
q.append((i - 1, j))
if i < m - 1 and heights[i + 1][j] == -1:
heights[i + 1][j] = curr_height
q.append((i + 1, j))
if j > 0 and heights[i][j - 1] == -1:
heights[i][j - 1] = curr_height
q.append((i, j - 1))
if j < n - 1 and heights[i][j + 1] == -1:
heights[i][j + 1] = curr_height
q.append((i, j + 1))
return heights