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 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.Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.Subscribe to see which companies asked this question
Show Tags
题目大意:给出一个矩阵,求矩阵中最大增序列的长度。
简单的思路
这道题可以看作一个四叉树的深度优先搜索。
为什么题目中的矩阵可以看作四叉树?因为两个相邻元素,只有大小不相等时,才会有一条边,从大的元素指向小的元素。这样连接之后不可能出现环,因为若有环存在,说明有a>b>…>a,显然不成立。连接完所有的边之后,以最小元素为根,上下左右四个邻居为子结点,形成了一颗四叉树。
想要求最大递增序列,就是从根节点到最深的叶子结点,也就是求这棵四叉树的深度。思路是遍历矩阵的元素,对于每一个元素,求出其相邻且比它更大的元素的深度,取其最大值加一后作为自己的深度。
采用普通的深度优先算法,需要递归调用,效率比较低。
递归过程:
如果该元素相邻元素可以访问(在边界内、比当前元素大),就依次求出邻居的深度,取最大值加一作为自己的深度。递归出口:没有可访问邻居,深度为1(只有自己)。
改进的思路
增加一个memo矩阵,作为记录。对于没有访问过的元素,置其值为0,代表没有访问过。对于访问的元素,求出当前元素的深度,存在memo中。
代码如下:
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.size() == )
return ;
int width = matrix[].size();
int height = matrix.size();
int result = ;
vector<vector<int>> memo(height, vector<int>(width, ));
for (int i = ; i < height; i++)
for (int j = ; j < width; j++)
{
int tmp = nprocess(matrix, i, j, height, width, memo);
if (tmp > result)
result=tmp;
}
return result;
} int nprocess(vector<vector<int>> &matrix, int i, int j, int m, int n, vector<vector<int>> &memo)
{
if (memo[i][j] != ) return memo[i][j];
memo[i][j] = ;
vector<vector<int>> shift{ { , - }, { , }, { -, }, { , } };
int max = ;
for (int k = ; k < ; k++)
{
int x = i + shift[k][];
int y = j + shift[k][];
if ((x < m) && (x >= ) && (y<n) && (y >= ) && (matrix[x][y]>matrix[i][j]))
{
int tmp = nprocess(matrix, x, y, m, n, memo) + ;
if (tmp > max)
max = tmp;
}
}
memo[i][j] = max;
return max;
}
};
总结
重点是对于递归过程的优化。
一个问题是,将矩阵转换成四叉树。我一开始的想法,是将矩阵转换成有向图,考虑到可能存在的环路i,在深度遍历中加入了一个矩阵表示该结点是否被访问过。事实是,这个矩阵不可能是有环路的有向图(不存在a>b>…>a),这样就退化成了一棵四叉树。所以,因为没有环路的存在,是否被访问过的结点在未加入记忆矩阵的深度优先中没有作用。
LeetCode #329. Longest Increasing Path in a Matrix的更多相关文章
- 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 ...
- [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 ...
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 ...
- [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization
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 ...
- 329. Longest Increasing Path in a Matrix
最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
随机推荐
- 泛函编程(38)-泛函Stream IO:IO Process in action
在前面的几节讨论里我们终于得出了一个概括又通用的IO Process类型Process[F[_],O].这个类型同时可以代表数据源(Source)和数据终端(Sink).在这节讨论里我们将针对Proc ...
- No.020:Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 配置云服务器 FTP 服务
自己配置的环境: OS: 阿里云 CentOS 6.5 >>Begin: 1. 登录到阿里云服务器(如何登录阿里云服务器), 在root权限下, 通过如下命令安装 vsftp [root@ ...
- Android异步回调中的UI同步性问题
Android程序编码过程中,回调无处不在.从最常见的Activity生命周期回调开始,到BroadcastReceiver.Service以及Sqlite等.Activity.BroadcastRe ...
- spring常用注解使用解析
spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件.spring将会把合适的java类全部注册成spring Bean. 问题:spring怎么知道把哪些 ...
- Oracle EBS Form Builder使用Java beans创建窗体
最近有个项目,需要研究一下Oracle的E-Business Sutie(EBS),对于以前没接触此套件的我来说,简直太痛苦了.在网上找了一堆资料,试着进行Form二次开发,也遇到各类奇葩问题.目前遇 ...
- 前端优秀作品展示,JavaScript 版水果忍者
<水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 Rapha ...
- HTML5拖放(drag and drop)与plupload的懒人上传
HTML5拖放能够将本地的文件拖放到页面上,plupload又是很好的文件上传插件,而今天就将两者结合,做了个文件拖拽上传的功能. 简述HTML5拖放 拖放是HTML5标准的一部分,任何元素都能够拖放 ...
- mysql 时间函数转换
1 NOW() //当前时间 2 SYSDATE() //当前时间 3 CURRENT_TIMESTAMP 4 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前的 ...
- 关于iOS中的时间
两类 绝对时间 [NSDate date].CFAbsoluteTimeGetCurrent(),或者gettimeofday(). 返回的是从某一个时刻开始,度过的秒数.会随着用户设置的系统时间更改 ...