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 ...
随机推荐
- 【Jenkins】远程调用jenkins进行构建方式!
前提:jenkins支持远程调用(具体设置自行百度)1.在我的个人中心--configure--API TOKEN--如果没有,则添加一个token,并生成,再复制并记录下来2.在你的job上面加上你 ...
- MeteoInfoLab脚本示例:站点数据绘制等值线
站点数据绘制等值线需要首先将站点数据插值为格点数据,MeteoInfo中提供了反距离权法(IDW)和cressman两个方法,其中IDW方法可以有插值半径的选项.这里示例读取一个MICAPS第一类数据 ...
- spring boot:接收数组参数及多文件混合json参数(spring boot 2.3.4)
一,生产环境中的复杂参数上传的场景 1,保存排序值 : 例如:某一件商品的多张展示图片排序,提交的排序值要和图片的id相对应 2,上传多张图片,图片要和指定的变量相对应 例如:在添加商品sku时, 需 ...
- spring boot:用spring security加强druid的安全(druid 1.1.22 / spring boot 2.3.3)
一,druid的安全保障有哪些环节要注意? 1,druid ui的访问要有ip地址限制 2,用户必须要有相应的权限才能访问druid 3,关闭重置功能 说明:stat-view-servlet.url ...
- centos8平台用ffprobe获取视频文件信息(ffmpeg4.2.2)
一,ffprobe的作用 ffprobe是强大的视频分析工具, 用于从多媒体流中获取相关信息或查看文件格式信息, 并以可读的方式打印 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...
- requests设置代理ip
# coding=utf-8 import requests url = "http://test.yeves.cn/test_header.php" headers = { &q ...
- 第三章 TCP/IP 模型
一.产生背景 1.伴随着计算机网络的飞跃发展,各大厂商根据自己的协议生产出了不同的硬件和软件 2.为了实现网络设备间的互相通讯,ISO和IEEE相继提出了OSI参考模型及其TCP/IP模型 二.TCP ...
- 【转】Centos7系统下忘记了root管理员账号密码的解决方式
哎,事情的起因就是脑子背了,曾经还手贱把root密码改了,导致普通账户改不回管理员账号了,然而,这次是百毒"救"了我,最终完美修改了root密码,好文章特地转载过来了,侵权删. 原 ...
- 面经手册 · 第14篇《volatile 怎么实现的内存可见?没有 volatile 一定不可见吗?》
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.码场心得 你是个能吃苦的人吗? 从前的能吃苦大多指的体力劳动的苦,但现在的能吃苦已经包括太 ...
- B. Rock and Lever 解析(思維)
Codeforce 1420 B. Rock and Lever 解析(思維) 今天我們來看看CF1420B 題目連結 題目 給一個數列\(a\),求有多少種\((i,j)\)使得\(i<j\) ...