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 ...
随机推荐
- MySQL中Redo Log相关的重要参数总结
参数介绍 下面介绍.总结一下MySQL的Redo Log相关的几个重要参数:innodb_log_buffer_size.innodb_log_file_size.innodb_log_files ...
- JSX 详解
一 jsx 的本质是什么? jsx是语法糖,需要被编译成js才能运行. jsx 看似是html 结构,实质是js结构的语法糖,在代码编译阶段被编译成js结构.所以jsx的本质可描述为看似html结构的 ...
- day24 Pyhton学习 反射
一.isinstance,type,issubclass issubclass() 这个内置函数可以帮我们判断x类是否是y类的子类 issubclass(x,y) class Base: pass c ...
- 非科班8k,靠这套知识体系收入暴涨100%!
我是18年毕业,非科班,毕业即进入互联网行业.坐标深圳,java程序员,当时到手薪资8k左右. bat等大厂月薪薪资动辄20k,25k,还不包括"签字费",福利和奖金.当然,薪资也 ...
- python 保存登录状态 cookie
import requests from lxml import etree import faker url = "https://www.yeves.cn/admin/Articles& ...
- Flink on Yarn三部曲之一:准备工作
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Vue内容
vue中的过滤器 moeths点击 过滤器的含义 过滤器就是把原有的数据过一遍 放到页面中 不会改变原有的数据 只是在原有的数据上增加新的数据
- 全网通4G工业路由器模块和串口转网口/4G/有线/WiFi/LTE模块的实现原理
随着现在信息化的高速发展,网络信息的需求量大增,在移动的4G流量的场合比如汽车上实现WiFi网络覆盖,户外wifi网络覆盖需求下,4G流量已经明显不够用,而网线到达的成本比较大,难以管控.在这市场痛点 ...
- v s
关键字volatile是线程同步的轻量级实现,所以volatile性能肯定比synchronized要好,并且只能修改变量,而synchronized可以修饰方法,以及代码块. ...
- 如何分析、排查、解决Redis变慢问题?
关于如何分析.排查.解决Redis变慢问题,根据实践总结了一些清单如下: 1.使用复杂度过高的命令(例如SORT/SUION/ZUNIONSTORE/KEYS),或一次查询全量数据(例如LRANGE ...