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 ...
随机推荐
- 内存管理2-set方法的内存管理
1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类 @age @book -------------------- #import "book.h" ...
- 工作流调度系统Azkaban的简介和使用
1 概述 1.1 为什么需要工作流调度系统 l 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 l 各任务单元之间存在时间 ...
- 图论——最短路:Floyd,Dijkstra,Bellman-Ford,SPFA算法及最小环问题
一.Floyd算法 用于计算任意两个节点之间的最短路径. 参考了five20的博客 Floyd算法的基本思想如下:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个 ...
- richtextbox Ctrl+V只粘贴纯文本格式
只能粘贴剪切板中的TXT内容 并且 不能改变 剪切板的内容1 当用户按下Ctrl+V屏蔽系统的粘贴功能,然后添加自己的功能2019年12月19日 19:34:38 private void richT ...
- Swift 条件语句
条件语句通过设定的一个或多个条件来执行程序,在条件为真时执行指定的语句,在条件为 false 时执行另外指定的语句. 可以通过下图来简单了解条件语句的执行过程: Swift 提供了以下几种类型的条件语 ...
- Editplus的运行JAVA的配置
工具--->参数设置
- Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in 解决方案:$sql = ...
- WPF 设置TextBox为空时,背景为文字提示。
<TextBox FontSize="> <TextBox.Resources> <VisualBrush x:Key="HelpBrush" ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_14.RabbitMQ研究-与springboot整合-搭建环境
我们选择基于Spring-Rabbit去操作RabbitMQ https://github.com/spring-projects/spring-amqp 使用spring-boot-starter- ...
- 反向代理远端 单台tomcat 使用ip+端口
.环境 nginx 10.1.1.161 公网:123.58.251.166 tomcat 10.1.1.103 .tomcat 配置 [root@host---- ~]# netstat -tnlp ...