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 ...
随机推荐
- yii2源码学习笔记(十)
继续了解Application. /** * Registers the errorHandler component as a PHP error handler. * 注册errorHandler ...
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
关于网页中第一行<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 解决android调用IIS Express中的WCF服务时,出现错误400问题
IIS Express仅支持localhost主机名地址访问. 找到IIS Express Config文件下的 applicationhost.confi 修改配置 再来调试android应用, ...
- Cocoapod安装 - 管理第三方库
在我们开发移动应用的时候,一般都会使用到第三方工具,而由于第三方类库的种类繁多,我们在项目中进行管理也会相对麻烦,所以此时我们就需要一个包管理工具,在iOS开发中,我们使用最多的就是Cocoapods ...
- Xcode7.01相对于底版本的变动小结
1.在Xcode7中系统不再自动支持http请求,需要配置plist才能使用http: 2.appdelegate中的self.window不再支持直接往window上加view,必须先给window ...
- Dynamips做CCNA的实验,说是找不到telnet的解决方案
01.如果你的系统是32位的系统. 控制面板-程序与功能-启动或关闭windows功能-开启telnet(重启计算机就可以用telnet了) 02.如果你的系统是64位的系统. (1)控制面板-程序与 ...
- 关于Java(介绍)
基于Java官方指导文档,开展学习 Java是什么 Java 是编程语言,也是一个平台 特性 简单 可移植 面向对象 分布式运算高性能 健壮 安全 动态 体系结构中立 记忆宫殿:恋爱是件简单的事,但放 ...
- front-end
http://info.1688.com/detail/1139720782.html http://segmentfault.com/q/1010000000136513 http://h5apps ...
- VCC,VDD,VEE,VSS,VPP 表示的意义
转自VCC,VDD,VEE,VSS,VPP 表示的意义 VCC,VDD,VEE,VSS,VPP 表示的意义 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, ...
- Eclipse中添加android sdk javadoc和source
在 javadoc location path中添加file:/D:/Android_SDK/sdk/docs/reference/ 在 source attachment中添加为 Externa ...