778. Swim in Rising Water
class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
N = len(grid)
direction = [0, 1, 0, -1, 0]
visited = set()
minH = [[grid[0][0], 0, 0]]
while minH:
t, r, c = heapq.heappop(minH)
if r == N - 1 and c == N - 1:
return t
for i in range(4):
newR, newC = r + direction[i], c + direction[i + 1]
if not (0 <= newR < N and 0 <= newC < N) or (newR, newC) in visited:
continue
visited.add((newR, newC))
heapq.heappush(minH, [max(t, grid[newR][newC]), newR, newC])