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(核心在于缓存遍历过程中的中间结果)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  6. 329. Longest Increasing Path in a Matrix

    最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...

  7. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question

    在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.

  8. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  9. 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 ...

随机推荐

  1. 【9】进大厂必须掌握的面试题-DevOps面试

    Q1.DevOps和Agile之间的根本区别是什么? 下表中列出了两者之间的差异. 特征 DevOps--开发运维 Agile--敏捷 敏捷 开发和运营中的敏捷性 只有发展才能敏捷 流程/实践 涉及C ...

  2. centos8用firewalld搭建防火墙

    一,firewalld的systemd管理命令 启动:systemctl start firewalld 关闭:systemctl stop firewalld 查看状态:systemctl stat ...

  3. matlab cvx工具箱解决线性优化问题

    题目来源:数学建模算法与应用第二版(司守奎)第一章习题1.4 题目说明 作者在答案中已经说明,求解上述线性规划模型时,尽量用Lingo软件,如果使用Matlab软件求解,需要做变量替换,把二维决策变量 ...

  4. MySql中varchar(10)和varchar(100)的区别

    背景 许多使用MySQL的同学都会使用到varchar这个数据类型.初学者刚开始学习varchar时,一定记得varchar是个变长的类型这个知识点,所以很多初学者在设计表时,就会把varchar(X ...

  5. win10+ubuntu18.04lts双系统安装葵花宝典(解疑篇)

    本文将对win10+ubuntu18.04lts双系统安装过程中的一些操作的目的和可能遇到的问题进行解释. 文章目录 如何正确分区 创建双系统后直接进入了windows怎么办 修改ubuntu开机界面 ...

  6. ERP仓库管理的操作与设计--开源软件诞生20

    赤龙ERP库房管理讲解--第20篇 用日志记录"开源软件"的诞生 [点亮星标]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redragon/r ...

  7. Html+Ajax+Webservice 实现文件跨域上传

    1. 界面HTML <p >上传文件: <input id="zfiles" type="file" name="file" ...

  8. Windows搭建Hexo系统

    date: 2018-11-16 17:10:51 updated: 2018-11-16 20:04:43 1.安装Git 下载Windows下的Git客户端并安装,安装很简单,基本一路Next下去 ...

  9. 双非本科拿到阿里腾讯字节,分享Java后端路线

    前言 最近有很多小师妹来问我. 师妹:师兄~看了你之前的从腾讯到阿里,最后选择字节,觉得你特别厉害,请问你是怎么进BAT的呀,能不能告诉我你的学习路线呀~ 既然小师妹都这么问了,那我在这篇就如实回答她 ...

  10. 如何計算n個圓的聯集面積

    如何計算n個圓的聯集面積 前言 一般人第一次遇到這個問題,可能會想要想辦法用排容原理,找圓之間交疊的凸包之類的.... 然而我只要舉一個例子,你就會發現我們就算把凸包找出來了,我們也非常難知道找到的凸 ...