329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径
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 矩阵中的最长递增路径的更多相关文章
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Java实现 LeetCode 329 矩阵中的最长递增路径
329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...
- Leetcode 329.矩阵中的最长递增路径
矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...
- 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 ...
- [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 ...
- [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 eit ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
随机推荐
- Uva 10730 Antiarithmetic?
uva 10730 题意:给出一列数字,如果其中存在长度大于等于3的等差数列,输出no,不存在就输出yes 这道题标定了数列长度n,而且这n个数数据范围是从0到n-1,没有相同的数,这就给我们枚举提供 ...
- 九度oj 题目1078:二叉树遍历
题目1078:二叉树遍历 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5326 解决:3174 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历 ...
- HDU 1130
题目大意 给定节点数 , 求通过这么多个节点能得到的二叉树的组成方式 用卡特兰数解决 f[n] = (4*n-2) * f[n-1] / (n+1); 递归不断解决 /** * @(#)Main.ja ...
- [luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)
传送门 由于 Di 只有 3 种情况,那么就很简单了 f[i][j][0] 表示前 i 个,且第 i 个变成 j 的 递增序列最小修改次数 f[i][j][1] 表示前 i 个,且第 i 个变成 j ...
- CodeForces 367E Sereja and Intervals
CodeForces 3 67E (109 + 7). Two ways are considered distinct if there is such j(1 ≤ j ≤ n), that the ...
- T1097 校门外的树 codevs
http://codevs.cn/problem/1097/ 题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的 ...
- Visual Studio 中的 .NET Framework 类库
Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...
- CI 日志类
开发ci的过程中,使用log能直观的看出代码运行到哪,还可设置代码查看数据接口的发送情况.日志类: <?php defined('BASEPATH') OR exit('No direct sc ...
- Linux进程空间分布 & 上下文
Linux使用两级保护机制:0级供内核使用,3级供用户程序使用.从图中可以看出,每个进程有各自的私有用户空间(0~3G),这个空间对系统中的其他进程是不可见的.最高的1GB字节虚拟内核空间则为所有进程 ...
- ZMQ源代码分析(一)-- 基础数据结构的实现
yqueue 和 ypipe zmq号称是"史上最快的消息队列",由此可见zmq中最重要的数据结构就是队列. zmq的队列主要由yqueue和ypipe实现.yqueue是队列的基 ...