Top 150 Interview簡単

Swim In Rising Water

Detailed guide and Python implementation for the 'Swim In Rising Water' problem.

問題提起

簡単

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

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?
Consider using Advanced Graphs-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。