LeetCode. 矩阵中的最长递增路径
题目要求:
给定一个整数矩阵,找出最长递增路径的长度。
对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
示例:
输入: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]。
class Solution {
public:
int dx[5] = {-1, 0, 1, 0};
int dy[5] = {0, 1, 0, -1};
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.empty()) return 0;
int max1 = 0;
vector<vector<bool>> visited(matrix.size()+1, vector<bool>(matrix[0].size()+1, false));
vector<vector<int>> len(matrix.size()+1, vector<int>(matrix[0].size()+1, 0));
for(int i = 0; i < matrix.size(); i++) {
for(int j = 0; j < matrix[0].size(); j++) {
max1 = max(max1, find(matrix, visited, len, i, j));
}
}
return max1;
}
int find(vector<vector<int>> matrix, vector<vector<bool>> visited, vector<vector<int>> len, int x, int y) {
if(visited[x][y]) {
return len[x][y];
}
len[x][y] = 1;
for(int i = 0; i < 4; i++) {
int _x = dx[i] + x;
int _y = dy[i] + y;
if(_x >= 0 && _x < matrix.size() && _y >= 0 && _y < matrix.size() && matrix[_x][_y] < matrix[x][y]) {
len[x][y] = max(len[x][y], find(matrix, visited, len, _x, _y) + 1);
}
}
visited[x][y] = true;
return len[x][y];
}
};
LeetCode. 矩阵中的最长递增路径的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Leetcode 329.矩阵中的最长递增路径
矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...
- Java实现 LeetCode 329 矩阵中的最长递增路径
329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 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 ...
- [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 ...
- 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 ...
- 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...
- 【题解】最长递增路径 [51nod1274]
[题解]最长递增路径 [51nod1274] 传送门:最长递增路径 \([51nod1274]\) [题目描述] 一个可能有自环有重边的无向图,每条边都有边权.输入两个整数 \(n,m\) 表示一共 ...
- 51nod1274 最长递增路径
将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<alg ...
随机推荐
- python3实现互信息和左右熵的新词发现--基于字典树
字典树 原来讲明白了剩下的就是具体实现了,最适合存储和计算词频的数据结构就是字典树,这里给一个讲解的很清楚的链接 具体代码 代码已开源,需要的点击这个Github
- nodejs基础(管道、流)实现:复制、压缩、加密、解压,解密,写入文件
stream流 都是events.EventEmitter的一个实例,都可以来创建自定义事件(也就是说,流是一个事件的实例) 在nodejs中 对http的请求与响应都是用流来实现的,请求就是一个输入 ...
- cxf报错 Cannot find any registered HttpDestinationFactory from the Bus.
错误信息:Cannot find any registered HttpDestinationFactory from the Bus. 报错主要是因为缺少jetty依赖 一般添加如下依赖即可 < ...
- WIN7+QT5.2.0 连接oracle11g问题及解决方法
用下面的代码建立连接之后,出现了几个问题 //连接数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QOCI"); /**连接Ora ...
- mysql 远程登陆
1.查询mysql是否启动 netstat -lnp|grep 3306 ps -df |grep mysqld 2.通过TCPIP的方式测试连接 mysql -uqingjiao -padm ...
- CCF认证历年试题
CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...
- 【Java/JDBC】借助ResultSetMetaData,从数据库表中抽取字段信息存成Excel文件
本例工程下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-3.rar 工作中曾有个为42张表建立测 ...
- Android——coredump解析
撰写不易,转载需注明出处:http://blog.csdn.net/jscese/article/details/46916869本文来自 [jscese]的博客! coredump文件生成前文And ...
- 50行代码写的一个插件,破解一个H5小游戏
小游戏链接:测测你的眼睛对色差的辨识度http://www.webhek.com/post/color-test.html?from=timeline 废话不多说,先放代码: window.onloa ...
- linux下如何使rtc设备注册为指定的设备文件/dev/rtc1?
答: 通过设备树中的aliases节点来指定即可; 如某rtc设备的节点名为rtc@68,那么想让系统为该设备生成指定的设备文件/dev/rtc1,那么就在设备树的根节点中增加aliases节点,示例 ...