#leetcode#329. Longest Increasing Path in a Matrix

来源:互联网 发布:软件开发基础教程pdf 编辑:程序博客网 时间:2024/06/11 06:00

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
原创粉丝点击