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. 多测师讲解自动化--rf断言(下)--_高级讲师肖sir

    1.Page Should Contain 1.1存在页面上的内容 1.2 不存在页面上的内容, 运行抛异常 1.3 Page Should Not Contain 1.4 1.5 输入页面元素,判断 ...

  2. day51 Pyhton 前端02

    内容回顾: 1.h1~h6:加粗,数字越大级别越小,自动换行 2.br:换行;hr:分割线; (特殊符号,空格) 3.p:与前边和后边内容之间有间距 4.a标签的href:本地文件连接;网络连接;锚链 ...

  3. 关于pipeline的一篇转载博文https://www.cnblogs.com/midhillzhou/p/5588958.html

    引用自https://www.cnblogs.com/midhillzhou/p/5588958.html 1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客 ...

  4. python xlrd读取Excel文件

    1 import xlrd 2 3 #打开excel文件 4 book = xlrd.open_workbook('salary.xls') 5 6 #打印每个工作表的名称 7 for sheet i ...

  5. python实现elasticsearch操作-CRUD API

    python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...

  6. C语言编程丨循环链表实现约瑟夫环!真可谓无所不能的C!

    循环链表   把链表的两头连接,使其成为了一个环状链表,通常称为循环链表. 和它名字的表意一样,只需要将表中最后一个结点的指针指向头结点,链表就能成环儿,下图所示.   需要注意的是,虽然循环链表成环 ...

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

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

  8. ASP.NET Core 3.1 Razor 视图预编译、动态编译

    1.安装NuGet包 Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 2.Startup.cs 配置 public ...

  9. gin教程

    Golang Gin 实战(十)| XML渲染 Golang Gin 实战(九)| JSONP跨域和劫持 Golang Gin 实战(八)| JSON渲染输出 Golang Gin 实战(七)| 分组 ...

  10. python并发编程之多进程(实践篇) 转

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了multiproce ...