Back to Practice Dashboard
Top 150 InterviewEasy

Swim In Rising Water

Learn how to solve the 'Swim In Rising Water' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

You are given an n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j). Rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if both elevations in the squares are at most t.

You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (n-1, n-1)?

Write a function swimInWater(grid: List[List[int]]) -> int.

Constraints
  • n == len(grid) == len(grid[i])
  • 1 <= n <= 50
  • 0 <= grid[i][j] < n^2
  • Each value grid[i][j] is unique

Examples

Example 1
Input
grid = [[0,2],[1,3]]
Output
3
Explanation

At time 3, you are permitted to swim all the way from (0,0) to (1,1). The path is 0 -> 1 -> 3.

Example 2
Input
grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output
16
Explanation

The final path is 0-1-2-3-4-5-16-15-14-13-12-11-10-9-8-7-6. The maximum height along this path is 16.

Need a Hint?
Represent graph node connections as an adjacency list/matrix, then use standard BFS or DFS graph traversal.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor