当前位置:网站首页>leetcode:329. 矩阵中的最长递增路径
leetcode:329. 矩阵中的最长递增路径
2022-07-01 14:53:00 【OceanStar的学习笔记】
题目来源
题目描述
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
}
};
题目解析
暴力递归
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; } // 因为下面已经检查了边界,所以这里可以去掉 */
// 上、下、左、右四种尝试
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;
// 上、下、左、右四种选择中选一种最大的(不能走回头路) + 自己
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) {
//每个位置出发试一遍
ans = std::max(ans, process(matrix, i, j));
}
}
return ans;
}
};
会超时
备忘录
class Solution {
int process(vector<vector<int>>& matrix, int i, int j, vector<vector<int>> &dp){
if(dp[i][j] != 0){
//如果为0,表示没有算过
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]不可能为0,哪怕只有自己也会是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;
}
};
为什么这道题记忆化搜索就结束了
本题依赖顺序是乱的,不好整理,只依赖有限的4个位置,没有必要再优化了。否则太花精力了
边栏推荐
- Details of appium key knowledge
- Research Report on development trend and competitive strategy of global consumer glassware industry
- Research Report on the development trend and competitive strategy of the global commercial glassware industry
- JVM performance tuning and practical basic theory part II
- Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
- 2022-2-15 learning the imitation Niuke project - post in Section 2
- tensorflow2-savedmodel convert to tflite
- 使用net core 6 c# 的 NPOI 包,讀取excel..xlsx單元格內的圖片,並存儲到指定服務器
- MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
- Build MySQL master-slave server under Ubuntu 14.04
猜你喜欢
What problems should be considered for outdoor LED display?
Build your own website (14)
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
The first technology podcast month will be broadcast soon
博文推荐 | 深入研究 Pulsar 中的消息分块
Opencv interpolation mode
Buuctf reinforcement question ezsql
Markdown编辑器使用基本语法
[dynamic programming] interval dp:p1005 matrix retrieval
The markdown editor uses basic syntax
随机推荐
C learning notes (5) class and inheritance
Storage form of in-depth analysis data in memory
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
Word2vec yyds dry goods inventory
What data capabilities do data product managers need to master?
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
Today, with the popularity of micro services, how does service mesh exist?
MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
tensorflow2-savedmodel convert to tflite
2022-2-15 learning xiangniuke project - Section 4 business management
微服务开发步骤(nacos)
关于重载运算符的再整理
Research Report on the development trend and competitive strategy of the global diamond suspension industry
Vnctf2022 open web gocalc0
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
Solidty智能合约开发-简易入门
What value can NPDP bring to product managers? Do you know everything?
tensorflow2-savedmodel convert to tflite
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
Minimum spanning tree and bipartite graph in graph theory (acwing template)