原题链接在这里:https://leetcode.com/problems/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.

题解:

Longest increasing path could start from any grid of matrix.

We could perform DFS starting from each grid of matrix. For each of 4 neighbors, if it is within boundary and number > current grid number, continue DFS.

Could use memoization to save time. If this grid has already calcualted maximum increasing path before, simply return it.

Time Complexity: O(mn). For DFS, it could take O(mn) time. But here use memoization, each grid could be visited no more than 5 times. m = matrix.length. n = matrix[0].length.

Space: O(m*n).用了memoization.

AC Java:

 class Solution {
HashMap<Integer, Integer> hm = new HashMap<>();
int [][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public int longestIncreasingPath(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
} int m = matrix.length;
int n = matrix[0].length;
int res = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
res = Math.max(res, dfs(matrix, m, n, i, j));
}
} return res;
} private int dfs(int [][] matrix, int m, int n, int i, int j){
int key = i*n+j;
if(hm.containsKey(key)){
return hm.get(key);
} int longest = 0;
for(int [] dir : dirs){
int x = i + dir[0];
int y = j + dir[1];
if(x>=0 && x<m && y>=0 && y<n && matrix[x][y]>matrix[i][j]){
longest = Math.max(longest, dfs(matrix, m, n, x, y));
}
} hm.put(key, longest+1);
return longest+1;
}
}

LeetCode Longest Increasing Path in a Matrix的更多相关文章

  1. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

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

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

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

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

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

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

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

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

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

  8. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

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

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

随机推荐

  1. 八、job管理

    查看用法: [root@super65 ~]# salt-run -d|grep jobs'jobs.active:' #查看当前执行的job Return a report on all activ ...

  2. 使用CSS3实现百叶窗

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  3. JavaScript - 倒计时

    http://www.helloweba.com/demo/loading/ WEB开发中经常会用到倒计时来限制用户对表单的操作,比如希望用户在一定时间内看完相关协议信息才允许用户继续下一步操作,又比 ...

  4. 2. MySQL

    安装: apt-get install mysql-server   配置: mysql_install_db mysql_secure_installation   另有两处需要修改 [mysqld ...

  5. OC中的属性、方法及内存管理

    普通方法:关注(代表)对象可以”干什么”,过程中需要实例变量.-(void)show;输出 … 访问属性    属性:属性专门处理实例变量.(程序执行过程当中)    初始化方法:一创建对象(第一时间 ...

  6. setInterval 启用和停止,见代码

    <title></title>    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="t ...

  7. [ZZ] 在windows上编译Mesa3d opengl32库

    在windows上编译Mesa3d opengl32库 cheungmine http://blog.csdn.net/ubuntu64fan/article/details/8061475 Mesa ...

  8. TextView实现圆角效果

    自定义一个Xml样式: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...

  9. 【翻译】CEDEC2012 SQUARE ENIX GPGPU实现高速GI烘培工具的方法

     虽然实时GI技术已经趋于成熟了,但出于对不同平台的性能和质量的考虑, 更倾向搭配一些预计算的渲染技术来实现,如给静态物体提供GI的LightMap, 给动态物体提供GI的Irradiance Vol ...

  10. php时间函数time(),date(),mktime()区别

    php时间函数time(),date(),mktime()区别   浏览:1161 发布日期:2014/12/18 分类:系统代码 关键字: php时间函数 time() date()mktime() ...