当前位置:网站首页>leetcode - 329. Longest increasing path in matrix
leetcode - 329. Longest increasing path in matrix
2022-06-28 04:09:00 【zmm_ mohua】
leetcode - 329. The longest incremental path in the matrix
subject

Code
#include <iostream>
#include <vector>
using namespace std;
/* Change your mind , Think of it as a graph , When two adjacent numbers are not equal , There will be an edge from a large value to a small value What we are looking for is the longest path of the graph , So use dfs */
int m, n;
int dir[4][2] = {
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}};
int dfs(vector<vector<int> >& matrix, vector<vector<int> >& memo, int i, int j){
if(memo[i][j] != 0){
return memo[i][j];
}
memo[i][j]++;
for(int k = 0; k < 4; k++){
int x = i + dir[k][0];
int y = j + dir[k][1];
if(x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]){
memo[i][j] = max(memo[i][j], dfs(matrix, memo, x, y) + 1);
}
}
return memo[i][j];
}
int longestIncreasingPath(vector<vector<int> >& matrix) {
if(matrix.size() == 0 || matrix[0].size() == 0){
return 0;
}
m = matrix.size();
n = matrix[0].size();
int res = 1;
vector<vector<int> > memo(m, vector<int>(n, 0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
res = max(res, dfs(matrix, memo, i, j));
}
}
return res;
}
int main(){
int m, n, res;
cin>>m>>n;
vector<vector<int> > matrix(m, vector<int>(n));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cin>>matrix[i][j];
}
}
res = longestIncreasingPath(matrix);
cout<<res;
return 0;
}
边栏推荐
- 第14章 AC-DC电源前级电路 笔记一
- 机器学习入门笔记
- 美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书
- 电学基础知识整理(一)
- How to learn a programming language systematically| Dark horse programmer
- 一文告诉你什么是 Kubernetes
- Problems with cat and dog queues
- 11_刻意练习精讲
- Reverse a stack with recursive functions and stack operations only
- Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
猜你喜欢

欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?

【小程序实战系列】电商平台源码及功能实现

Learning notes of digital circuit (II)

黑体辐射初探

Understanding and learning of parental delegation mechanism

English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions

视频爆炸时代,谁在支撑视频生态网高速运行?

ambari SSLError: Failed to connect. Please check openssl library versions.

简单工厂模式

MSC 307(88) (2010 FTPC Code)第2部分烟气和毒性测试
随机推荐
English grammar_ Adjective / adverb Level 3 - Comparative
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
Chapter 14 AC-DC power supply front stage circuit note I
[graduation season] graduate summary
Analysis of future teacher research ability under steam education framework
Open the field of maker education and creation
How to apply for ASTM E108 flame retardant test for photovoltaic panels?
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
Learning notes of digital circuit (II)
使用tensorboard进行loss可视化
applicationContext.getBeansOfType 获取一个接口下所有实现类 执行方法或者获取实现类对象等 操作应用场景学习总结
几个重要的物理概念
04 MongoDB各种查询操作 以及聚合操作总结
揭开SSL的神秘面纱,了解如何用SSL保护数据
基于arm5718的Shell脚本参数传递的2种方法
Two methods of shell script parameter passing based on arm5718
A solution to the inefficiency of setting debug mode in developing flask framework with pychar
ambari SSLError: Failed to connect. Please check openssl library versions.
MySQL master-slave replication, separation and resolution
《性能之巅第2版》阅读笔记(二)--CPU监测