329. Longest Increasing Path in a Matrix
最后更新
三刷?
找矩阵里的最长路径。
看起来是DFS,实际上也就是。但是如果从每个点都进行一次DFS然后保留最大的话,会超时。
这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰到的时候,如果已经算过,可以直接调取这个值。
用空间交换了部分时间。
写的时候我吸取教训,把边界判断放在DFS的开始。。
Time Complexity: 不会算。。 O(4mn)?因为只能单方向= =,每个点往一个方向延伸,就不可能回来。
Space : O(m*n)
public class Solution {
public int longestIncreasingPath(int[][] matrix) {
if (matrix.length == 0) return 0;
int row = matrix.length;
int col = matrix[0].length;
int[][] dp = new int[row][col];
int res = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
res = Math.max(res, dfs(i, j, matrix, dp, Integer.MIN_VALUE));
}
}
return res;
}
public int dfs(int i, int j, int[][] matrix, int[][] dp, int prev) {
if (i < 0 || j < 0 || i >= matrix.length || j >= matrix[0].length) return 0;
if (matrix[i][j] <= prev) return 0;
if (dp[i][j] != 0) return dp[i][j];
int temp = matrix[i][j];
int tempMax = 0;
tempMax = Math.max(tempMax, dfs(i+1, j, matrix, dp, temp));
tempMax = Math.max(tempMax, dfs(i, j+1, matrix, dp, temp));
tempMax = Math.max(tempMax, dfs(i-1, j, matrix, dp, temp));
tempMax = Math.max(tempMax, dfs(i, j-1, matrix, dp, temp));
dp[i][j] = tempMax + 1;
return dp[i][j];
}
}
一刷
一看是H难度的就害怕了,以为有什么特别的办法,动态规划之类的,结果是DFS。。。那就没啥难度了。。
甚至连VISIT都不用,因为一次遍历只可能越来越大。。不可能出现unvisited but goable的情况。。
DP记录下每个格的最大距离,避免重复计算就行了。
代码可以更简单,有些中间变量可以胜率,不过那样一行太长了。。
public class Solution
{
public int longestIncreasingPath(int[][] matrix)
{
if(matrix.length == 0) return 0;
int[][] dp = new int[matrix.length][matrix[0].length];
int res = 0;
for(int i = 0; i < dp.length;i++)
{
for(int j = 0; j < dp[0].length;j++)
{
if(dp[i][j] == 0)
{
dp[i][j] = helper(matrix,dp,i,j)+1;
}
res = Math.max(res,dp[i][j]);
}
}
return res;
}
public int helper(int[][] matrix, int[][] dp, int m, int n)
{
if(dp[m][n] != 0) return dp[m][n];
int res = 0;
int temp = matrix[m][n];
if(m > 0 && matrix[m-1][n] > temp)
{
if(dp[m-1][n] == 0) dp[m-1][n] = helper(matrix,dp,m-1,n) + 1;
res = Math.max(res,dp[m-1][n]);
}
if(n > 0 && matrix[m][n-1] > temp)
{
if(dp[m][n-1] == 0) dp[m][n-1] = helper(matrix,dp,m,n-1) + 1;
res = Math.max(res,dp[m][n-1]);
}
if(m+1 < dp.length && matrix[m+1][n] > temp)
{
if(dp[m+1][n] == 0) dp[m+1][n] = helper(matrix,dp,m+1,n) + 1;
res = Math.max(res,dp[m+1][n]);
}
if(n+1 < dp[0].length && matrix[m][n+1] > temp)
{
if(dp[m][n+1] == 0) dp[m][n+1] = helper(matrix,dp,m,n+1) + 1;
res = Math.max(res,dp[m][n+1]);
}
dp[m][n] = res;
return dp[m][n];
}
}
今天晕晕乎乎的,这个题做得也不好。
DFS+DPmemory
要注意什么时候更新dp[i][j]
public class Solution {
public int longestIncreasingPath(int[][] matrix) {
if (matrix.length == 0) return 0;
int[][] dp = new int[matrix.length][matrix[0].length];
int res = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (dp[i][j] == 0) {
dp[i][j] = dfs(matrix, i, j, dp) + 1;
}
res = Math.max(res, dp[i][j]);
}
}
return res;
}
public int dfs(int[][] matrix, int m, int n, int[][] dp) {
int temp = matrix[m][n];
int res = 0;
if (m < matrix.length - 1 && temp < matrix[m+1][n]) {
if (dp[m+1][n] == 0) {
dp[m+1][n] = dfs(matrix, m+1, n, dp) + 1;
}
res = Math.max(res, dp[m+1][n]);
}
if (n < matrix[0].length - 1 && temp < matrix[m][n+1]) {
if (dp[m][n+1] == 0) {
dp[m][n+1] = dfs(matrix, m, n+1, dp) + 1;
}
res = Math.max(res, dp[m][n+1]);
}
if (m > 0 && temp < matrix[m-1][n]) {
if (dp[m-1][n] == 0) {
dp[m-1][n] = dfs(matrix, m-1, n, dp) + 1;
}
res = Math.max(res, dp[m-1][n]);
}
if (n > 0 && temp < matrix[m][n-1]) {
if (dp[m][n-1] == 0) {
dp[m][n-1] = dfs(matrix, m, n-1, dp) + 1;
}
res = Math.max(res, dp[m][n-1]);
}
dp[m][n] = res;
return res;
}
}
329. Longest Increasing Path in a Matrix的更多相关文章
- 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 ...
- leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...
- [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 eit ...
- 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...
- 329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Longest Increasing Path in a Matrix -- LeetCode 329
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- setInterval和setTimeout定时器
1,文本框自增(重零开始)每隔一秒递增1 <input type="text" name="name" value="0" id=&q ...
- curl采集 根据关键词 获取雅虎竞价排名
之前写过curl批处理采集数据,这里贴上完整版本,代码很简单,废话不说,上代码,新手欢迎指教!!! 代码只写到 获取到链接了,至于排名 后边数组的键不就是排名喽... <?php /** * B ...
- mongodb数据库操作--备份 还原 导出 导入
首先数据库备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 mongodump -h 127.0.0.1 -u admin -p ...
- 2D image convolution
在学习cnn的过程中,对convolution的概念真的很是模糊,本来在学习图像处理的过程中,已对convolution有所了解,它与correlation是有不同的,因为convolution = ...
- Python Web 性能和压力测试 multi-mechanize
http://www.aikaiyuan.com/5318.html 对Web服务做Performance & Load测试,最常见的工具有Apache Benchmark俗称ab和商用工具L ...
- Mysql table ful
http://ourmysql.com/archives/1327 http://blog.csdn.net/kevon_sun/article/details/7967728 http://my.o ...
- Linux Screen命令使用
参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...
- 《转》SQL Server 2008 数据维护实务
SQL Server 2008 数据维护实务 http://blog.csdn.net/os005/article/details/7739553 http://www.cnblogs.com/xun ...
- 【Xamarin开发 Android 系列 4】 Android 基础知识
原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...
- 安卓高手之路之java层Binder
很多人一提到Binder就说代理模式,人云亦云的多,能理解精髓的少. 本篇文章就从设计角度分析一下java层BInder的设计目标,以及设计思路,设计缺陷,从而驾驭它. 对于[邦德儿]的理解, 从通信 ...