Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

Example

Example 1:

Input: nums = 
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
] 
Output: 4 
Explanation: The longest increasing path is [1, 2, 6, 9].

Example 2:

Input: nums = 
[
  [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.

Note

Given a matrix:

[ [1 ,2 ,3 ,4 ,5], [16,17,24,23,6], [15,18,25,22,7], [14,19,20,21,8], [13,12,11,10,9] ] return 25

类似与滑雪问题 这道题很容易想到二维dp, 记录f[i][j] 是以i,j为结尾的LIS。但是由于我们走的方向不仅仅是从左往右和从上往下, 还可能从右往左和从下往上, 所以

这样的问题, 我们只能dfs加记忆化搜索. 每个位置最长的LIS记录在一个矩阵dp[][] 里面, 同时用一个flag[][] 矩阵记录下遍历过没有. dp和flag矩阵可以写在一起, 但是最好不要, 因为dp代表当前最长长度, flag代表遍历过没有, 意义不同. 这道题的时间复杂度是O(n*n), 因为有flag, 每个点最多遍历一次.

(其实不需要flag,每个点其实也只是访问一次因为找路径)

Code

Last updated