Swim In Rising Water
Detailed guide and Python implementation for the 'Swim In Rising Water' problem.
Problem Statement
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.
- •n == len(grid) == len(grid[i])
- •1 <= n <= 50
- •0 <= grid[i][j] < n^2
- •Each value grid[i][j] is unique
Examples
grid = [[0,2],[1,3]]
3
At time 3, you are permitted to swim all the way from (0,0) to (1,1). The path is 0 -> 1 -> 3.
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]]
16
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?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Try/Except & Error Handling
Prevent your Python scripts from crashing. Learn try, except, finally blocks and how to raise custom exceptions properly.
How to Reverse a String in Python
Learn how to reverse a string in Python using slicing, the reversed() function, and loop concatenation with visual code examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.