最后更新

三刷?

找矩阵里的最长路径。

看起来是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. Laravel Repository 模式

    Repository 模式 为了保持代码的整洁性和可读性,使用Repository Pattern 是非常有用的.事实上,我们也不必仅仅为了使用这个特别的设计模式去使用Laravel,然而在下面的场景 ...

  2. MYSQL数据库备份与恢复

    mysqldump -h主机名  -P端口 -u用户名 -p密码 (–database) 数据库名 > 文件名.sql 备份MySQL数据库的命令 mysqldump -hhostname -u ...

  3. C++ static全局变量与全局变量的区别/static全局函数与全局函数的区别

    全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...

  4. OrderedDict

    OrderedDict 使用dict时,Key是无序的.在对dict做迭代时,我们无法确定Key的顺序. 如果要保持Key的顺序,可以用OrderedDict: >>> from c ...

  5. Things About 'extern'

    Note: All Learned From Here C和Objective-C的function前面都有个隐含的extern,对于function来说,有没有extern都无所谓,但变量不一样. ...

  6. Django中国|Django中文社区——python、django爱好者交流社区

    Django中国致力于成为Python和Django框架等技术的中文开发者学习交流平台. 内容涵盖python教程.python基础.Django教程.python入门.web.py教程.linux教 ...

  7. libiconv2.dll

    一.问题描述 在我使用MinGW的mingw32-make工具的时候,提示错误“libiconv-2.dll找不到”. 二.问题解决 1.从脚本之家下载“libiconv-2.dll”,下载地址“ht ...

  8. 将FAT32格式转换成NTFS格式命令

    convert f: /fs:ntfs /x 可以不用格式化里面的数据

  9. Aircrack-ng官方文档翻译[中英对照]---Aireplay-ng

    Aircrack-ng官方文档翻译---Aireplay-ng[90%] Description[简介] Aireplay-ng is used to inject frames. Aireplay- ...

  10. Java数据库连接关闭后无法启动

    错误如下: java.sql.SQLException: No operations allowed after connection closed. at com.mysql.jdbc.Connec ...