150强访谈中等

矩阵中最长的递增路径

“矩阵中最长递增路径”问题的详细指南和 Python 实现。

问题陈述

中等

给定一个 m x n 整数矩阵,返回矩阵中最长递增路径的长度。

在每个单元格中,您可以向四个方向移动:左、右、上或下。您不得沿对角线移动或移出边界(即不允许环绕)。

编写一个函数 longestIncreasingPath(matrix: List[List[int]]) -> int

约束条件
  • m == len(matrix)
  • n == len(matrix[0])
  • 1 <= m, n <= 200
  • 0 <= matrix[i][j] <= 2^31 - 1

示例

Example 1
Input
matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output
4
Explanation

The longest increasing path is [1, 2, 6, 9].

Example 2
Input
matrix = [[3,4,5],[3,2,6],[2,2,1]]
Output
4
Explanation

The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

Need a Hint?
考虑使用 2D DP 特定的数据结构,例如集合或堆。
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 资源

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