150强访谈简单

在上升的水中游泳

“在上升的水中游泳”问题的详细指南和 Python 实现。

问题陈述

简单

给定一个 n x n 整数矩阵网格,其中每个值 grid[i][j] 表示该点 (i, j) 的海拔。雨开始下。在t时刻,各处水深为t。当且仅当方格中的两个高程都至多为 t 时,你才能从一个方格游到另一个 4 方向相邻的方格。

从左上角的方块 (0, 0) 开始。到达右下方格 (n-1, n-1) 所需的最短时间是多少?

编写一个函数 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?
考虑使用高级图特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。