15% off all plans & roadmaps Use code
See plansLeetCode 778 · Solution & editorial
You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j).
Swim in Rising Water is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Dijkstra's algorithm for finding shortest paths in weighted graphs with non-negative edges.
The reference solution runs in O(n^2) time and O(n^2) space.
Swim in Rising Water has been reported in interviews at Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Swim in Rising Water is a Graph Traversal Patterns (DFS & BFS) problem, in the Shortest Path (Dijkstra's Algorithm) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.