原题链接在这里: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. PHP 常用到的一些小程序

    1.计算两个时间的相差几天 $startdate=strtotime(“2009-12-09”); $enddate=strtotime(“2009-12-05”); 上面的php时间日期函数strt ...

  2. List 中对象属性排序

    有几个方法可以实现:让 Student 实现Comparable接口,或是实例化一 个比较器, 现在用 Comparator 比较器实例来做一个:ComparableTest.java import  ...

  3. wordpress安装

    通过浏览器访问wordpress文件包 点击现在就开始,填写下面内容 我的填写 如出现下面情况,你得先创建一个数据库,再重试 数据库的创建 之后会出现 点击进行安装 安装成功 登录 主界面 写个文章, ...

  4. thinkphp 代码执行

    相关漏洞:http://loudong.360.cn/vul/info/id/2919 ThinkPHP 开启lite模式后,会加载ThinkPHP/Extend/Mode/Lite/Dispache ...

  5. 自制Chrome拓展

    淘宝试用自动点击: 谷歌其实就是一些html+css+js+静态资源.但是里面有一个特别的配置文件manifest.json.该文件和Android的那个androidmanifest.xml类似,记 ...

  6. Unattend.xml应答文件制作(WISM)-- 转自爱做梦的鱼

    将制作好的应答文件unattend.xml拷贝到模板机sysprep目录下,然后在cmd下运行(unattend.xml文件可自定义名称)   sysprep /generalize /oobe /s ...

  7. Linux常用命令(持续更新中)

    cd 目录名 :进入某个目录 ls :列出当前目录的内容 locate 文件名/目录名:寻找文件.目录 find 目录名1 -name 文件名/目录名2 :在目录1中寻找目录2 whereis  文件 ...

  8. Python的包管理工具--PIP安装使用

    最新安装方式 # wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py  // 使用该方式安装已经不再要求提前安装setuptoo ...

  9. 使用CodeIgniter框架搭建RESTful API服务

    使用CodeIgniter框架搭建RESTful API服务 发表于 2014-07-12   |   分类于 翻译笔记   |   6条评论 在2011年8月的时候,我写了一篇博客<使用Cod ...

  10. nginx location 匹配顺序

    location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … } “=”是精确匹配“@”是命名的location ,在正常的location 匹配中不会使用, ...