329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)
Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
Input: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
Explanation: The longest increasing path is[1, 2, 6, 9].
Example 2:
Input: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
Output: 4
Explanation: The longest increasing path is[3, 4, 5, 6]. Moving diagonally is not allowed.
class Solution {
public:
//如果不保存计算过程中的结果,就会超时。利用dp[i][j]来保存矩阵matrix[i][j]这个位置作为起始位置的最长路径的长度。
bool judgeValid(int x,int y,vector<vector<int>>& matrix){
return x >= 0 && x <= matrix.size()-1 && y >=0 && y <= matrix[0].size()-1;
}
//x,y当前元素坐标。maxLen全局最长路径
int dfs(vector<vector<int>>& matrix,int x,int y,vector<vector<int>> &dp){
if(dp[x][y]) return dp[x][y];
int maxLen = 1;
vector<vector<int>> dirs = {{1,0},{-1,0},{0,1},{0,-1}};
for(auto dir:dirs){
int xx = x+dir[0];
int yy = y+dir[1];
if(!judgeValid(xx,yy,matrix) || matrix[xx][yy] <= matrix[x][y]) continue;
int len = 1+dfs(matrix,xx,yy,dp);
maxLen = max(len,maxLen);
}
dp[x][y] = max(maxLen,dp[x][y]);
return dp[x][y];
}
//要回溯
int longestIncreasingPath(vector<vector<int>>& matrix) {
if(matrix.size() == 0) return 0;
if(matrix[0].size() == 0) return 0;
int resLen = 1;
vector<vector<int>> dp(matrix.size(),vector<int>(matrix[0].size(),0));
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[0].size();j++){
int len = dfs(matrix,i,j,dp);
resLen = max(len,resLen);
}
}
return resLen;
}
};
329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)的更多相关文章
- LeetCode #329. Longest Increasing Path in a Matrix
题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...
- leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...
- [LeetCode] 329. 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 ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 329. Longest Increasing Path in a Matrix
最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Longest Increasing Path in a Matrix -- LeetCode 329
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- golang不想http自动处理重定向的解决方案
目录 前言 解决方案 结论 前言 有时候发送http请求不想让库自动帮忙处理重定向,库里面默认的是会把所有重定向都完成一遍,结果就是最后一个没有重定向的请求的结果.因此需要一种方案直接获取首次 ...
- python -re库
正则表达式的语法 正则表达式语法由字符和操作符构成 正则表达式的常用操作符: print("--正则表达式常用操作符--") mata="11356352135 abcd ...
- ubuntu下vi不能正常使用的解决方法
现象 ubuntu中vi在编辑状态下方向键不能用,还有回格键不能删除等我们平时习惯的一些键都不能使用. 原因 ubuntu预装的是vim tiny版本,需要的是vim full版本. 方案 $sudo ...
- git merge 与 git rebase的区别?
一,git merge 与 git rebase的区别 1,git merge 例如: master分支合并dev分支,git将两个分支dev和master上的所有commit , 按照提交时间的先后 ...
- centos8安装php7.4
一,下载php7.4 1,官方网站: https://www.php.net/ 2,下载 [root@yjweb source]# wget https://www.php.net/distribut ...
- centos8平台使用pstree查看进程树
一,pstree用途 Linux pstree命令将所有行程以树状图显示,树状图将会以 pid (如果有指定) 或是以 systemd 这个基本行程为根 (root) 说明:centos6及更旧版本为 ...
- D - 活动选择
Description 学校的大学生艺术中心周日将面向全校各个学院的学生社团开放,但活动中心同时只能供一个社团活动使用,并且每一个社团活动开始后都不能中断.现在各个社团都提交了他们使用该中心的活动计划 ...
- IDEA提示类注释的wrong tag警告的解决办法
当创建一个类的时候,给类写上注释时,出现类似于"Wrong tag 'date' less... (Ctrl+F1)"这种警告,解决方式是: alt + enter -> a ...
- 4. Validator校验器的五大核心组件,一个都不能少
困难是弹簧,你弱它就强.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众号[BAT的 ...
- 安卓WebSocket使用
引入jar包: implementation "org.java-websocket:Java-WebSocket:1.4.0"implementation "org.s ...