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:

  1. nums = [
  2. [9,9,4],
  3. [6,6,8],
  4. [2,1,1]
  5. ]

Return 4
The longest increasing path is [1, 2, 6, 9].

Example 2:

  1. nums = [
  2. [3,4,5],
  3. [3,2,6],
  4. [2,2,1]
  5. ]

Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

  1. int dx[] = { 1 , -1, 0 , 0 };
  2. int dy[] = { 0 , 0 , 1 , -1 };
  3. class Solution {
  4. public:
  5. int dfs(int x, int y, const int &m,const int &n,vector<vector<int>>& matrix, vector<vector<int>>& dis) {
  6. if (dis[x][y]) return dis[x][y];
  7.  
  8. for (int i = 0; i < 4; i++) {
  9. int nx = x + dx[i];
  10. int ny = y + dy[i];
  11. if (nx >= 0 && ny >= 0 && nx < m && ny < n && matrix[nx][ny] > matrix[x][y]) {
  12. dis[x][y] = max(dis[x][y], dfs(nx, ny, m, n, matrix, dis));
  13. }
  14. }
  15. return ++dis[x][y];
  16. }
  17.  
  18. int longestIncreasingPath(vector<vector<int>>& matrix) {
  19. if (!matrix.size()) return 0;
  20. int m = matrix.size(), n = matrix[0].size();
  21. vector<vector<int> > dis(m, vector<int>(n, 0));
  22. int ans = 0;
  23. for (int i = 0; i < m; i++) {
  24. for (int j = 0; j < n; j++) {
  25. ans = max(ans, dfs( i, j, m, n, matrix, dis));
  26. }
  27. }
  28. return ans;
  29. }
  30. };

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

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

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

  7. 329. Longest Increasing Path in a Matrix

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

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

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

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

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

  10. [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

随机推荐

  1. PHP-GTK的demo在windows下运行出现的问题

    I am trying to use Firebird 2.5.2.26539 with wamp,When i enable the extensions of firebird in php: - ...

  2. Strand Specific mRNA sequencing 之重要性与分析

    Strand Specific mRNA sequencing 之重要性与分析 发表评论 2,761 A+ 所属分类:Bioinformatics   收  藏 研究生物基因转录体的方法有许多种,而使 ...

  3. Netty 零拷贝(一)NIO 对零拷贝的支持

    Netty 零拷贝(二)NIO 对零拷贝的支持 Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) 非直接缓冲区(HeapBy ...

  4. [转]关于docker包存储结构说明

    原文:http://blog.csdn.net/w412692660/article/details/49005631 前段时间与同事交流docker的安装包层次结构,并沟通相关每个文件的作用,但是一 ...

  5. hadoop学习笔记(一):概念和组成

    一.什么是hadoop Apache Hadoop是一款支持数据密集型分布式应用并以Apache 2.0许可协议发布的开源软件框架.它支持在商品硬件构建的大型集群上运行的应用程序.Hadoop是根据G ...

  6. Docker mysql启动自动按顺序导入sql

    1.目录结构 -rw-r--r-- root root Jan : Dockerfile -rw-r--r-- root root Jan : initdb.sh drwxr-xr-x root ro ...

  7. python pip国内源

    pip国内的这个源最快   清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 修改源方法: 临时使用: 可以在使用pip的时候在后面加上-i参数,指定pip ...

  8. Spring bean是如何加载的

    Spring bean是如何加载的 加载bean的主要逻辑 在AbstractBeanFactory中doGetBean对加载bean的不同情况进行拆分处理,并做了部分准备工作 具体如下 获取原始be ...

  9. OpenGL常用的函数

    OpenGL常用的函数 1. void glBegin(GLenummode) void glEnd(void) 参数说明: mode:创建图元的类型.可以是以下数值 GL_POINTS:把每一个顶点 ...

  10. Linux sort uniq 命令。简单运用

    -n                              #代表以数字方法排序,如果倒序加上-r -t ':'                          #-t指定分隔符-k       ...