↓Skip to main content
  1. LeetCode/

LeetCode 778: Swim in Rising Water

·3 mins· ·
LeetCode Hard Graph Dijkstra Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-04-18 Source: Day 7 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

    1. Explain why LC 778 is a Dijkstra variant.

Learning Note Extract
#

Problem 1 - LC 778 Swim in Rising Water
#

  • Status: Completed; prefer direct Dijkstra wording in interviews.
  • Pattern: Dijkstra variant on grid.
  • Graph type: Grid graph; each cell is a node.
  • Path cost: Maximum elevation value along the path.
  • State in heap: (time_required, row, col).
  • Return condition: Return when bottom-right cell is popped from heap.

Interview-Ready Explanation
#

This is a shortest-path problem on a grid graph. The cost of a path is the maximum elevation value of any cell on that path, because at time t, I can only enter cells with elevation <= t. Dijkstra works because when I extend a path to a neighbor, the new cost is max(current_time, grid[nr][nc]), so the path cost never decreases. I store (time_required, row, col) in a min-heap. When a cell is popped for the first time, its minimum required time is finalized. When the destination is popped, I return that time.

Preferred Implementation Detail
#

Prefer pushing the full path cost:

new_time = max(time, grid[nr][nc])
heappush(heap, (new_time, nr, nc))

This matches the Dijkstra explanation directly.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from heapq import heappop, heappush
from typing import List

class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
        n = len(grid)
        seen = [[False] * n for _ in range(n)]
        heap = [(grid[0][0], 0, 0)]
        dirs = [(1,0), (-1,0), (0,1), (0,-1)]

        while heap:
            time, r, c = heappop(heap)
            if seen[r][c]:
                continue
            seen[r][c] = True
            if (r, c) == (n - 1, n - 1):
                return time
            for dr, dc in dirs:
                nr, nc = r + dr, c + dc
                if 0 <= nr < n and 0 <= nc < n and not seen[nr][nc]:
                    heappush(heap, (max(time, grid[nr][nc]), nr, nc))
        return -1

Complexity
#

Time O(n^2 log n), Space O(n^2).

Mistakes To Watch
#

  • Summing elevations.
  • Using plain BFS despite different required times.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 1203: Sort Items by Groups Respecting Dependencies
·3 mins
LeetCode Hard Graph Topological-Sort
LeetCode note for Sort Items by Groups Respecting Dependencies, rebuilt from the original learning note
LeetCode 1514: Path with Maximum Probability
·3 mins
LeetCode Medium Graph Dijkstra Shortest-Path Heap
LeetCode 1514 note: multiplying probabilities only shrinks them, so Dijkstra applies; prune with <=
LeetCode 1793: Maximum Score of a Good Subarray
·3 mins
LeetCode Hard Two-Pointers Greedy Binary-Search Array Monotonic-Stack
LeetCode 1793 note: expand from k toward the larger neighbor each step so the minimum stays as large as possible for each length; at a boundary, only the other side can grow
LeetCode 2972: Count the Number of Incremovable Subarrays II
·2 mins
LeetCode Hard Two-Pointers Binary-Search Array
LeetCode 2972 note: what remains is a strictly increasing prefix plus suffix; for each valid prefix end, a right-only pointer finds the first suffix start that can follow it, and every later start works too
LeetCode 1782: Count Pairs Of Nodes
·3 mins
LeetCode Hard Two-Pointers Graph Sorting Hash-Table Counting
LeetCode 1782 note: count pairs with deg[a] + deg[b] > q using sorted degrees and converging pointers, then correct the over-count for pairs that share edges
LeetCode 2302: Count Subarrays With Score Less Than K
·2 mins
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
LeetCode 2302 note: score = sum × length with positive numbers grows with the window; shrink while the score is >= k, and right - left + 1 valid subarrays end at right