最后更新

三刷?

找矩阵里的最长路径。

看起来是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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  7. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question

    在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.

  8. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  9. 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 ...

随机推荐

  1. wdcp日志

    apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...

  2. php分页笔记

    在做留言板的时候,用到了分页,所以写了这个分页笔记   既然已经开始写分页了,肯定掌握了了php的一些知识以及mysql的基本操作   在做分页的时候,我也遇到了很多问题,但是大家不要怕,无论什么问题 ...

  3. 【 java版坦克大战--事件处理】 键盘控制小球上下左右移动

    上一节已经学习了事件处理,这一节需要完成通过键盘的上下左右键控制小球移动. 然后再通过应用到我们绘制的坦克上. /** * 加深对事件处理机制的理解 * 通过光标的上下左右键,控制小球的左右上下移动. ...

  4. jquery插件----文件上传uploadfile

    使用了一款jquery上传的插件,ajax上传,可以显示上传的进度,高可配性,简要记录. 插件地址http://hayageek.com/docs/jquery-upload-file.php git ...

  5. 简单的介绍下WPF中的MVVM框架

    最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...

  6. 安装Cocoa 新的依赖管理工具Carthage

    Cocoa的依赖管理器,我们已经有了CocoaPods,非常好用,那么为什么还要创建这样一个项目呢?本文翻译自Carthage的Github的README.md,带大家来了解一下这个工具有何不同之处. ...

  7. ~/.vimrc config

    runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...

  8. 自设chrome默认滚动条样式

    今天无聊,想着chrome这种全面使用html的浏览器,可不可以让我自行改变它的默认CSS呢,结果去查查,有,很好 win7/8目录为 C:\Users\[你的用户名]\AppData\Local\G ...

  9. asp.net学习资源汇总

    名称:快速入门地址:http://chs.gotdotnet.com/quickstart/描述:本站点是微软.NET技术的快速入门网站,我们不必再安装.NET Framework中的快速入门示例程序 ...

  10. 基础canvas应用-钟表绘制

    首先,canvas语法基础薄弱的小伙伴请点这里,剩下的小伙伴们可以接着往下看了. 一个表,需要画什么出来呢:3条线(时分秒针),1个圆(表盘),以及60条短线/点(刻度). 嗯,没毛病. 那接下来让我 ...