#leetcode#329. 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 1:

nums = [
  [9,9,4],
  [6,6,8],
  [2,1,1]
]

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

Example 2:

nums = [
  [3,4,5],
  [3,2,6],
  [2,2,1]
]

Return 4

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


---------------------------------------------------------------------------------------------------------------------------------


这题第一思路是DFS,对于每个点出发都数longest increasing path,代码如下

public class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return 0;
        int res = Integer.MIN_VALUE;
        int[][] cache = new int[matrix.length][matrix[0].length];
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[0].length; j++){
                int max = helper(matrix, i, j, Integer.MIN_VALUE, cache);
                res = Math.max(res, max);
            }
        }
        
        return res;
    }
    
    private int helper(int[][] matrix, int i, int j, int val, int[][]cache){
        if(i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length)
            return 0;
        if(cache[i][j] != 0){
            return cache[i][j];
        }
        if(matrix[i][j] <= val){
            return 0;
        }
        int up = helper(matrix, i - 1, j, matrix[i][j], cache);
        int down = helper(matrix, i + 1, j, matrix[i][j], cache);
        int left = helper(matrix, i, j - 1, matrix[i][j], cache);
        int right = helper(matrix, i, j + 1, matrix[i][j], cache);
        
        return Math.max(Math.max(up, down), Math.max(left, right)) + 1;
    }
}
时间复杂度是O(2^(m+n))

Complexity Analysis

  • Time complexity :
    O(2^{m+n})

    The search is repeated for each valid increasing path. In the worst case we can have
    O(2^{m+n}) calls. For example:

1 23 . . . n

2 3. . .   n+1

3 . . .     n+2

.           .

.           .

.           .

m m+1. . . n+m-1

  • Space complexity :
    O(mn). For each DFS we need O(h) space used by the system stack, whereh is the maximum depth of the recursion. In the worst case,  O(h) = O(mn)
怎么看出来O(2^{m+n})的呢?这里引用了leetcode解题分析的极端例子,上面那个matrix,递增只有向右或者向下2个方向,左上角的1看做binary tree的root, 那么这个tree的depth就是m+n, 复杂度就是 2^(m + n) + 2^(m + n - 1) + ... + 2^2 + 2 ^ 1.  可以看成是 m * n * 2^(m + n). m*n忽略,也就是O(2^{m+n})了,指数级,exponential.


如何优化呢? 从[0][0]出发, 到[0][1]会做一遍从[0][1]出发的DFS,然后for循环到[0][1]后,有会做一次DFS,显然重复计算了, 所以可以用memorization来优化复杂度, 用一个二维数组int[][] cache来记录从当前位置DFS得到的longest increasing path length,存进去, 那后面再有需要到这个点遍历就可以直接取出值来用, 没有重复访问,复杂度是O(m*n);
代码如下
public class Solution {
    public int longestIncreasingPath(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return 0;
        int res = Integer.MIN_VALUE;
        int[][] cache = new int[matrix.length][matrix[0].length];  
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[0].length; j++){
                int max = helper(matrix, i, j, Integer.MIN_VALUE, cache);
                res = Math.max(res, max);
            }
        }
        
        return res;
    }
    
    private int helper(int[][] matrix, int i, int j, int val, int[][]cache){
        if(i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length)
            return 0;
        if(matrix[i][j] <= val){
            return 0;
        }
        if(cache[i][j] != 0){
            return cache[i][j] + 1;
        }else{
            int up = helper(matrix, i - 1, j, matrix[i][j], cache);
            int down = helper(matrix, i + 1, j, matrix[i][j], cache);
            int left = helper(matrix, i, j - 1, matrix[i][j], cache);
            int right = helper(matrix, i, j + 1, matrix[i][j], cache);
            
            int max =  Math.max(Math.max(up, down), Math.max(left, right));
            cache[i][j] = max;
            return max + 1;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值