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- ...
随机推荐
- vb 运行ppt示例代码
来源:http://support.microsoft.com/kb/222929 通过使用 PowerPoint 中的自动运行功能,您可以以编程方式打印.显示幻灯片及执行以交互式执行的大多数事情.按 ...
- Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)
Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...
- HDU 1561 树形DP背包问题
这是自己第一道背包上树形结构问题,不是很理解这个概念的可以先看看背包九讲 自己第一次做,看了一下别人的思路,结合着对简单背包问题的求解方式自己一次AC了还是有点小激动的 题目大意是: 攻克m个城市,每 ...
- Uva10305 Ordering Tasks
John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...
- codevs1197 Vigenère密码
题目描述 Description 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère 密码的加密解密算法简单易用,且破译难度比 ...
- windows开启3306端口并用可视化工具访问远程mysql(授权访问)
开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 3306 端口,否则远程是无法通过 330 ...
- pij——1125 Stockbroker Grapevine
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37154 Accepted: ...
- BZOJ(4) 1050: [HAOI2006]旅行comf
1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3984 Solved: 2236[Submit][St ...
- delphi不同版本字符串类型的演化
string,DELPHI2009以前的版本string=ansistring,一个字符占一个字节,DELPHI2009及以上版本string=unicodestring,一个字符占二个字节. cha ...
- 服务器Hot Spare热备
Hot Spare:热备用 当一个正在使用的磁盘发生故障后,一个空闲.加电并待机的磁盘将马上代替此故障盘,此方法就是热备用.热备用磁盘上不存储任何的用户数据,最多可以有8个磁盘作为热备用磁盘.一个热备 ...