Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
nums = [
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
Return 4
The longest increasing path is [1, 2, 6, 9].
Example 2:
nums = [
  [3,4,5],
  [3,2,6],
  [2,2,1]
]
Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

详见:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/

Java 实现:

dp[i][j] 表示当前i, j 位置能都到的最大距离。
dp[i][j] 是通过dfs 来选的,初始dp[i][j] 都是0, 终止条件是若是走到了一个不是0的位置,那么直接返回dp[x][y]. 可以避免重复计算. 若是dp[x][y]已经有值,说明这个点4个方向的最大值已经找到, 找到dp[x][y]的路径必定是比matrix[x][y]小的,直接返回dp[x][y] 再加上 1 就是之前位置的最大延展长度了。
若是当前位置是0, 就从上下左右四个方向dfs, 若是过了边界或者新位置matrix[x][j] <= 老位置matrix[i][j], 直接跳过continue.
不然len = 1 + dfs. 取四个方向最大的len作为dp[i][j].
Time Complexity: 对于每一个点都做dfs, dfs O(m*n). 所以一共 O(m*n * m*n) = O(m^2 * n^2).
Space: O(m*n).用了dp array.

public class Solution {
final int [][] fourDirs = {{-1, 0}, {1,0}, {0,-1}, {0,1}}; public int longestIncreasingPath(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int max = 1;
int m = matrix.length;
int n = matrix[0].length;
int [][] dp = new int[m][n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
dp[i][j] = dfs(matrix, i, j, dp);
max = Math.max(max, dp[i][j]);
}
}
return max;
} private int dfs(int [][] matrix, int i, int j, int [][] dp){
if(dp[i][j] != 0){
return dp[i][j];
}
int max = 1;
int m = matrix.length;
int n = matrix[0].length;
for(int k = 0; k<fourDirs.length; k++){
int x = i+fourDirs[k][0];
int y = j+fourDirs[k][1];
if(x<0 || x>=m || y<0 || y>=n || matrix[x][y] <= matrix[i][j]){
continue;
}
int len = 1 + dfs(matrix, x, y, dp);
max = Math.max(max, len);
}
dp[i][j] = max;
return dp[i][j];
}
}

C++实现:

class Solution {
public:
vector<vector<int>> dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int longestIncreasingPath(vector<vector<int>>& matrix)
{
if (matrix.empty() || matrix[0].empty())
{
return 0;
}
int res = 1, m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
res = max(res, dfs(matrix, dp, i, j));
}
}
return res;
}
int dfs(vector<vector<int>> &matrix, vector<vector<int>> &dp, int i, int j)
{
if (dp[i][j])
{
return dp[i][j];
}
int mx = 1, m = matrix.size(), n = matrix[0].size();
for (auto a : dirs)
{
int x = i + a[0], y = j + a[1];
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] <= matrix[i][j])
{
continue;
}
int len = 1 + dfs(matrix, dp, x, y);
mx = max(mx, len);
}
dp[i][j] = mx;
return mx;
}
};

参考:https://www.cnblogs.com/grandyang/p/5148030.html

329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径的更多相关文章

  1. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

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

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

  3. Java实现 LeetCode 329 矩阵中的最长递增路径

    329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...

  4. Leetcode 329.矩阵中的最长递增路径

    矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...

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

  6. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

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

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

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

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

随机推荐

  1. react入门--------安装react

    创建一个单页面应用 Create React App是开始构建新的React单页应用程序的最佳方式. 它可以帮助您快速集成您的开发环境,以便您可以使用最新的JavaScript功能,它提供了一个很好的 ...

  2. 【模板】51nod 1006 最长公共子序列Lcs

    [题解] dp转移的时候记录一下,然后倒着推出答案即可. #include<cstdio> #include<cstring> #include<algorithm> ...

  3. PAT 1133 Splitting A Linked List

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  4. Ajax_数据格式_HTML

    [数据格式提要] 1.在服务器端Ajax是一门与语言无关的技术.在业务逻辑层使用何种服务器端语言都可以. 2.从服务器端接收数据的时候,那些数据必须以浏览器能够理解的格式来发送.服务器端的编程语言只能 ...

  5. java方法的虚分派和方法表

    java:方法的虚分派(virtual dispatch)和方法表(method table) Java方法调用的虚分派 虚分配(Virtual Dispatch) 首先从字节码中对方法的调用说起.J ...

  6. HDU 4902 (牛叉的线段树)

    Nice boat Problem Description There is an old country and the king fell in love with a devil. The de ...

  7. Swift对象创建过程(PUT Object)——纠删码策略(二)

    相应Object使用纠删码(EC)作为存储策略时,BaseObjectController类中PUT和GET需要调用的一些方法会被ECObjectController中相应函数覆盖. 在GET Obj ...

  8. vue.js中的路由vue-router2.0使用

    在我们平时工作中,我们有时候会有需求,按照不同的规则,加载不同的组件,页面不去跳转,常见的操作是ajax的异步操作,实现局部刷新加载新数据 在vue中,我们写了很多不同的组件,这时候,实现不刷新调用新 ...

  9. LeetCode240:Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  10. 性能测试实战-XYB项目-外网访问

    压测业务选择 跟产品.开发负责人评估系统中需要压测的重要业务接口 考虑到考勤业务是每天老师都需要做的且可多次考勤,列入压测重要业务中 值日检查也是每天老师都需要操作的业务,最终选择了考勤业务及值日检查 ...