当前位置:网站首页>leetcode:329. Longest increasing path in matrix
leetcode:329. Longest increasing path in matrix
2022-07-01 14:54:00 【Oceanstar's study notes】
Title source
Title Description



class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
}
};
title
Recurrence of violence
class Solution {
int process(vector<vector<int>>& matrix, int i, int j){
// base case
/*if(i < 0 || i == matrix.size() || j < 0 || j == matrix[0].size()){ return 0; } // Because the boundary has been checked below , So here we can get rid of */
// On 、 Next 、 Left 、 Right four attempts
int up = i > 0 && matrix[i - 1][j] > matrix[i][j] ? process(matrix, i - 1, j) : 0;
int down = i < matrix.size() - 1 && matrix[i + 1][j] > matrix[i][j] ? process(matrix, i + 1, j) : 0;
int left = j > 0 && matrix[i][j - 1] > matrix[i][j] ? process(matrix, i, j - 1) : 0;
int right = j < matrix[0].size() - 1 && matrix[i][j + 1] > matrix[i][j] ? process(matrix, i, j + 1) : 0;
// On 、 Next 、 Left 、 Choose the largest of the four choices on the right ( You can't go back ) + own
return std::max(up, std::max(down, std::max(left, right))) + 1;
}
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int ans = 0;
int m = matrix.size(), n = matrix[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
// Start from each position and try again
ans = std::max(ans, process(matrix, i, j));
}
}
return ans;
}
};
Will timeout
Memorandum
class Solution {
int process(vector<vector<int>>& matrix, int i, int j, vector<vector<int>> &dp){
if(dp[i][j] != 0){
// If 0, It means that you have not calculated
return dp[i][j];
}
int up = i > 0 && matrix[i - 1][j] > matrix[i][j] ? process(matrix, i - 1, j, dp) : 0;
int down = i < matrix.size() - 1 && matrix[i + 1][j] > matrix[i][j] ? process(matrix, i + 1, j, dp) : 0;
int left = j > 0 && matrix[i][j - 1] > matrix[i][j] ? process(matrix, i, j - 1, dp) : 0;
int right = j < matrix[0].size() - 1 && matrix[i][j + 1] > matrix[i][j] ? process(matrix, i, j + 1, dp) : 0;
return dp[i][j] = std::max(up, std::max(down, std::max(left, right))) + 1;
}
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int ans = 0;
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dp(m, std::vector<int>(n, 0));// dp[i][j] It can't be 0, Even if only I can be 1
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = std::max(ans, process(matrix, i, j, dp));
}
}
return ans;
}
};
Why is the memory search of this problem over
The dependent order of this question is chaotic , It's hard to tidy up , Rely only on Limited 4 A place , There is no need to optimize . Otherwise, it takes too much energy
边栏推荐
- Don't want to knock the code? Here comes the chance
- Opencv mat class
- 写在Doris毕业后的第一天
- 2022-2-15 learning the imitation Niuke project - post in Section 2
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- [dynamic programming] interval dp:p1005 matrix retrieval
- What is the relationship between network speed, broadband, bandwidth and traffic?
- Fundamentals of C language
- Generate random numbers (4-bit, 6-bit)
- Solid basic structure and array, private / public function, return value and modifier of function, event
猜你喜欢

关于重载运算符的再整理

Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界

241. 为运算表达式设计优先级

Vnctf2022 open web gocalc0

opencv学习笔记六--图像拼接

竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测

opencv学习笔记四--银行卡号识别
![[leetcode 324] swing sorting II thinking + sorting](/img/cb/26d89e1a1f548b75a5ef9f29eebeee.png)
[leetcode 324] swing sorting II thinking + sorting

Sqlachemy common operations

Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
随机推荐
Ubuntu 14.04下搭建MySQL主从服务器
Advanced C language
Develop small programs and official account from zero [phase III]
Research Report on the development trend and competitive strategy of the global display filter industry
It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
Apk signature principle
音乐播放器开发实例(可毕设)
Blog recommendation | in depth study of message segmentation in pulsar
tensorflow2-savedmodel convert to pb(frozen_graph)
Mongodb second call -- implementation of mongodb high availability cluster
Research Report on development trend and competitive strategy of global vibration polishing machine industry
Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
Error-tf. function-decorated function tried to create variables on non-first call
【阶段人生总结】放弃考研,参与到工作中,已经顺利毕业了,昨天刚领毕业证
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
Minimum spanning tree and bipartite graph in graph theory (acwing template)
Build your own website (14)
Sqlachemy common operations