LeetCode Longest Increasing Path in a Matrix
原题链接在这里: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的更多相关文章
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 ...
- 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 ☆☆☆
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [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 ...
- 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 ...
随机推荐
- 一个关于el中获取对象属性的错误
具体过程描述为: 在JSP页面中通过<%%>定义一个非public类.并定义两个属性相应的get和set方法.在该代码段中实例化该对象,并将该对象放入Request域中. 然后在页面中使用 ...
- ASP.NET WEB API的服务托管(Self-HOST)
如果我们想对外发布RESTful API,可以基于ASP.NET来构建Restful APIs,但需要部署IIS吗?答案是不必.你可以把它托管到一个Windows Service.具体如何把WEB A ...
- 通过SEP禁用USB
1 Introduction 1.1 Scope This document provides comprehensive information of the reinforce ...
- 【新产品发布】【iHMI43 智能液晶模块 2013 版】
iHMI43智能液晶模块 2013 版改进内容: 本着精益求精的态度,新版(2013版) iHMI43 模块发布了,在保证了与老版本(2012版)软件.机械尺寸兼容的情况下,改进了部分电路,使接口更合 ...
- JavaWEB入门
首先,我们已知的是Web资源分为:静态web资源(如html 页面):指web页面中供人们浏览的数据始终是不变.常用的静态web开发技术即为html:动态web 资源:指web页面中供人们浏览的数据是 ...
- 前端网址收集!Amazing! 神奇!
(1)Bootstrap中文网 http://www.bootcss.com/ (2)前端编码规范 http://codeguide.bootcss.com/ (3)bootstrap可视化布局+下载 ...
- django通过middleware计算每个页面的详细执行时间
你可以自定义一个MiddleWare类,然后在settings.py引用这个中间件,添加到MIDDLEWARE_CLASSES里,然后在公共模板里添显示代码即可. 添加到公共模板里的代码: <d ...
- 解决Windows照片查看器中图片显示发黄的问题
这其实是ICC颜色配置的问题,发生在Windows7自动更新下载了显示器的驱动后,自动安装后显示器的颜色配额制文件自动改为新下载的配置,导致显卡和显示器颜色配置不兼容的问题,不过不用担心,非常容易解决 ...
- 总结android项目的基本开发步骤(转帖)
总结android项目的基本开发步骤(转帖) 做了几个android企业应用项目后,总结了项目的基本开发步骤,希望能够交流.一 应用规划: ※确定功能. ※必须的界面及界面跳转的流程. ...
- nginx下增加模块
1.使用nginx -V确定nginx的编译参数2.下载nginx源码3.下载nginx的扩展模块(此处为memcache模块)4.进入nginx源码目录5../configure --prefix ...