原题链接在这里: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. RN组件之ListView

    /** * Created by DaGuo on 2016/4/7. */ 'use strict' import React,{ Component, View, Text, ListView, ...

  2. event.keycode大全(javascript)

    keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...

  3. Spark - RDD(弹性分布式数据集)

    org.apache.spark.rddRDDabstract class RDD[T] extends Serializable with Logging A Resilient Distribut ...

  4. FXK Javascript

    Javascript是一门神奇的语言,很不爽的一门语言,很纠结的一门语言. 以下内容,专业人士请不要看,只供像我一样的菜鸟参考. (1)Javascript找不到函数.明明已经引用了JS文件,却提示找 ...

  5. Bootstrap - 全局css样式类

    状态类 通过这些状态类可以为行或单元格设置颜色. .active 鼠标悬停在行或单元格上时所设置的颜色 .success 标识成功或积极的动作 .info 标识普通的提示信息或动作 .warning ...

  6. hadoop MapReduce 笔记

    1.        MapReduce程序开发步骤 编写map 和 reduce 程序–> 单元测试 -> 编写驱动程序进行验证-> 本地数据集调试 ->  部署到集群运行 用 ...

  7. 第二章、 Linux 如何学习

    第二章. Linux 如何学习 最近更新日期:2009/08/06 1. Linux当前的应用角色 1.1 企业环境的利用 1.2 个人环境的使用 Linux当前的应用角色 在第一章Linux是什么当 ...

  8. [SHELL进阶] (转)最牛B的 Linux Shell 命令 (二)

    1.用你最喜欢的编辑器来敲命令 command <CTRL-x CTRL-e> 在已经敲完的命令后按 <CTRL-x CTRL-e> ,会打开一个你指定的编辑器(比如vim,通 ...

  9. MVC4发布到IIS7报404错误

    在MVC根目录的web.config中添加 <system.webServer> <modules runAllManagedModulesForAllRequests=" ...

  10. QQ群笔记

     uuid就好比你的名字,类似到了班级里,你的名字会被学号替代.同样的连接之后,uuid会被handle句柄替代.   问下CC2541串口用DMA接收的时候,调试程序时候发现,串口发一帧数据,进入两 ...