#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)
如何优化呢? 从[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; } }}
- leetcode Longest Increasing Path in a Matrix
- [LeetCode] Longest Increasing Path in a Matrix
- leetcode: Longest Increasing Path in a Matrix
- Leetcode: Longest Increasing Path in a Matrix
- leetcode:Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- [Leetcode]Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix | leetcode
- LeetCode Longest Increasing Path in a Matrix
- 【Leetcode】Longest Increasing Path in a Matrix
- LeetCode:Longest Increasing Path in a Matrix
- [leetcode]Longest Increasing Path in a Matrix
- Leetcode Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- Leetcode 329. Longest Increasing Path in a Matrix
- LeetCode—329. Longest Increasing Path in a Matrix
- [leetcode] - 329. Longest Increasing Path in a Matrix
- 329. Longest Increasing Path in a Matrix LeetCode
- cacls命令设置文件及其文件夹权限的方法
- (caicloud) tensorboard 可视化—9.3监督指标可视化
- Python中for循环和while循环
- C语言小知识点
- DES ,AES加密解密的实现
- #leetcode#329. Longest Increasing Path in a Matrix
- daplink usb full-speed/high-speed到底差距多大
- Leetcode学习(15)—— Remove Linked List Elements
- 这是我的第一篇博客
- Hdu 1754 . I Hate It
- cors跨域资源共享方法
- CSU 1506 Double Shortest Paths【最小费用最大流】
- 【Android View事件分发机制】滑动冲突
- Jquery on方法绑定事件后执行多次