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 ...
随机推荐
- kubernetes案例 tomcat+mysql
该文章参考<kubernetes 权威指南> 环境: [root@master tomcat-mysql]# kubectl get nodesNAME STATUS AG ...
- instr和like的使用区别
1.instr函数 instr函数是一个字符串处理函数,它在Oracle/PLSQL中是返回子字符串在源字符串中的位置,如果在源串中没有找到子串,则返回0. instr函数定义如下: /* * 返回子 ...
- ORM 数据库使用
使用 Flask-SQLAlchemy 来操作数据库 1 配置 本文使用sqlite来作为例子演示,在config.py里面更新下数据库的配置 import os basedir = os.path ...
- PostgreSQL判断一个表是否存在
postgresql判断一个表是否存在: 方法一: select count(*) from pg_class where relname = 'tablename'; 方法二: select cou ...
- CloudFlare 新手入门中文教程
loudFlare成立于2009年,是国外著名的免费CDN网站加速服务公司,CloudFlare 还提供实时安全保护服务和网络优化等,采用的是免费+增值模式,可以免费使用,也有收费服务.国内也有很多免 ...
- Node.js与VUE安装及环境配置之Windows篇
Node.js安装及环境配置之Windows篇 https://www.cnblogs.com/zhouyu2017/p/6485265.html Node.js安装及环境配置之Windows篇htt ...
- python select模块
Python select 一.前言 Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句 ...
- 安装Oracle11g出现INS-13001环境不满足最低要求
原版:https://blog.csdn.net/Q_Sea__/article/details/79012808 第一次安装Oracle11g,就出现这个问题,就找了一些解决方案.现在总结一下. 出 ...
- 关于Jmeter测试移动端应用时提示非法登录,不是合法的登录设备时的解决办法
当Jmeter测试移动端应用时提示非法登录,不是合法的登录设备时的解决办法:只需要在jmeter的http信息头管理器中配置相应的设备信息,可通过抓包工具得到:即头信息Header中的Miscella ...
- Centos 6.8环境下OpenLDAP安装与部署
一.OpenLDAP介绍 LDAP 全称轻量级目录访问协议(英文:Lightweight Directory Access Protocol),是一个运行在 TCP/IP 上的目录访问协议.LDAP实 ...