最后更新

三刷?

找矩阵里的最长路径。

看起来是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. js与uri中location关系

    //获取域名host = window.location.host;host2=document.domain; //获取页面完整地址url = window.location.href; docum ...

  2. jquery验证网址格式

    在input中输入网址,用jquery验证输入网址是否正确 <input type="text" name="input-web" class=" ...

  3. 【Asp.Net】小BUG汇总[更新]

    目录结构 1.Dictionary<T>遍历 2.Asp.net网站部署在C盘无法上传下载文件 3.Asp.Net网站发布后远程无法访问 4.GDI+中发生一般性错误 1.Dictiona ...

  4. ABAP写的一个递归

    需求:计算下面树形结构中每个子节点与最上层父节点的对应关系. DATA:BEGIN OF lt_ztab OCCURS 0,      a  TYPE string,      b  TYPE str ...

  5. hdu 1827

    强连通分量——tarjin算法: 这题的思路就是找出多少个出度为0的连通分量,结果就是这些连通分量的元素的最小值相加: 一道很简单的题,改了我好久,= =!~ 贴代码: #include<cst ...

  6. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  7. 【网络流24题】 No.22~24

    接下来几题就写写题解吧.不是很想打了. 22. 输入文件示例input.txt4 21 2 7 36 5 8 37 8 10 59 6 13 9 输出文件示例output.txt17 最长不相交路径. ...

  8. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...

  9. SpringSecurity的简单应用(一)

    java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downlo ...

  10. html表格cell合并插件

    数据展示时需要合并部分数据自己写了一个简单插件 合并前: 合并后: 调用示例: var trs = $('table#dataList tbody tr').not('#demo').get(); v ...