[LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
2016-01-21 13:54
537 查看
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:
Return
The longest increasing path is
Example 2:
Return
The longest increasing path is
这道题给我们一个二维数组,让我们求矩阵中最长的递增路径,规定我们只能上下左右行走,不能走斜线或者是超过了边界。那么这道题的解法要用递归和DP来解,用DP的原因是为了提高效率,避免重复运算。我们需要维护一个二维动态数组dp,其中dp[i][j]表示数组中以(i,j)为起点的最长递增路径的长度,初始将dp数组都赋为0,当我们用递归调用时,遇到某个位置(x, y), 如果dp[x][y]不为0的话,我们直接返回dp[x][y]即可,不需要重复计算。我们需要以数组中每个位置都为起点调用递归来做,比较找出最大值。在以一个位置为起点用DFS搜索时,对其四个相邻位置进行判断,如果相邻位置的值大于上一个位置,则对相邻位置继续调用递归,并更新一个最大值,搜素完成后返回即可,参见代码如下:
参考资料:
https://leetcode.com/discuss/81389/15ms-concise-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
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.
这道题给我们一个二维数组,让我们求矩阵中最长的递增路径,规定我们只能上下左右行走,不能走斜线或者是超过了边界。那么这道题的解法要用递归和DP来解,用DP的原因是为了提高效率,避免重复运算。我们需要维护一个二维动态数组dp,其中dp[i][j]表示数组中以(i,j)为起点的最长递增路径的长度,初始将dp数组都赋为0,当我们用递归调用时,遇到某个位置(x, y), 如果dp[x][y]不为0的话,我们直接返回dp[x][y]即可,不需要重复计算。我们需要以数组中每个位置都为起点调用递归来做,比较找出最大值。在以一个位置为起点用DFS搜索时,对其四个相邻位置进行判断,如果相邻位置的值大于上一个位置,则对相邻位置继续调用递归,并更新一个最大值,搜素完成后返回即可,参见代码如下:
class Solution { public: 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]; vector<vector<int> > dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; 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://leetcode.com/discuss/81389/15ms-concise-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- 数据结构算法经典合集
- kali安装zmap
- SessionFilter的简单使用
- Android 自定义控件入门篇之自定义验证码
- 2015 年 Ruby 大盘点
- 工作中遇到的oracle分页查询问题及多表查询相关
- Unable to add window -- token android.os.BinderProxy@164db98f is not valid解决办法f
- chrdevs数组
- Eclipse js文件选中变量名,相同变量都变色显示的设置
- office2007每次打开都要配置文件,怎么取消配置
- swift数组与字典
- 责任链模式(chain)—员工请假
- 使用HttpClient进行远程接口测试
- 5个常用的Java分布式缓存框架
- RDD创建彻底解密
- Solr4.10使用教程(三):solr crud
- Shell Script 基础
- angularjs中的e2e测试实例
- 集合初探(七)
- Objective-C的Runtime机制的应用示例总结