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:

nums = [
[9,9,4],
[6,6,8],
[2,1,1]
]

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

Example 2:

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

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

第一种方法,递归。很明显,时间超时,通不过。

 class Solution {
public int longestIncreasingPath(int[][] matrix) {
if (matrix == null || matrix.length == || matrix[].length == ) return ;
int[] max = new int[];
boolean[][] visited = new boolean[matrix.length][matrix[].length];
for (int i = ; i < matrix.length; i++) {
for (int j = ; j < matrix[].length; j++) {
helper(matrix, visited, i, j, matrix[i][j], true, , max);
}
}
return max[];
} public void helper(int[][] matrix, boolean[][] visited, int i, int j, int prev, boolean isStart, int size, int[] max) {
if (i < || i >= matrix.length || j < || j >= matrix[].length || visited[i][j]) return;
if (matrix[i][j] <= prev && !isStart) return; visited[i][j] = true;
size++;
max[] = Math.max(max[], size); helper(matrix, visited, i + , j, matrix[i][j], false, size, max);
helper(matrix, visited, i - , j, matrix[i][j], false, size, max);
helper(matrix, visited, i, j + , matrix[i][j], false, size, max);
helper(matrix, visited, i, j - , matrix[i][j], false, size, max);
visited[i][j] = false;
}
}

第二种方法类似第一种方法,但是我们不会每次都对同一个位置重复计算。对于一个点来讲,它的最长路径是由它周围的点决定的,你可能会认为,它周围的点也是由当前点决定的,这样就会陷入一个死循环的怪圈。其实并没有,因为我们这里有一个条件是路径上的值是递增的,所以我们一定能够找到一个点,它不比周围的值大,这样的话,整个问题就可以解决了。

 public class Solution {
public int longestIncreasingPath(int[][] A) {
int res = ;
if (A == null || A.length == || A[].length == ) {
return res;
}
int[][] store = new int[A.length][A[].length];
for (int i = ; i < A.length; i++) {
for (int j = ; j < A[].length; j++) {
if (store[i][j] == ) {
res = Math.max(res, dfs(A, store, i, j));
}
}
}
return res;
} private int dfs(int[][] A, int[][] store, int i, int j) {
if (store[i][j] != ) {
return store[i][j];
}
int left = , right = , up = , down = ;
if (j + < A[].length && A[i][j + ] > A[i][j]) {
right = dfs(A, store, i, j + );
}
if (j > && A[i][j - ] > A[i][j]) {
left = dfs(A, store, i, j - );
}
if (i + < A.length && A[i + ][j] > A[i][j]) {
down = dfs(A, store, i + , j);
}
if (i > && A[i - ][j] > A[i][j]) {
up = dfs(A, store, i - , j);
}
store[i][j] = Math.max(Math.max(up, down), Math.max(left, right)) + ;
return store[i][j];
}
}

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

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

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

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

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

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

  5. LeetCode Longest Increasing Path in a Matrix

    原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...

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

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

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

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

随机推荐

  1. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 包含https 非对称秘钥 NB

    JavaWeb学习总结(三)--Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...

  2. 关于centos7的网络配置

    1.DNS DNS是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能 ...

  3. 使用idea开发Springmvc

    使用IntelliJ IDEA开发SpringMVC网站(一)开发环境 http://my.oschina.net/gaussik/blog/385697?fromerr=A4EKE0Ix idea1 ...

  4. jQuery EasyUI API 中文文档 - ComboGrid 组合表格

    jQuery EasyUI API 中文文档 - ComboGrid 组合表格,需要的朋友可以参考下. 扩展自 $.fn.combo.defaults 和 $.fn.datagrid.defaults ...

  5. 使用DOS比较两个txt文件的差异

    将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异

  6. flask 知识点总结

    ============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...

  7. [设计模式] javascript 之 迭代子模式

    迭代子模式:定义 迭代子模式,又称游标模式,是一种用于对聚集进行顺序访问规则的模式,是一种行为模式:它用于提供对聚集对象的一种统一的访问接口,使客户能够在不了解聚集对象内部结构的情况对聚集对象进行访问 ...

  8. Linq to sql 的语法

    Linq to SQL 语法查询(子查询 & in操作 & join ) 引用地址:http://www.cnblogs.com/82767136/articles/2949541.h ...

  9. 使用 Flexbox 的居中布局

  10. OC第四节——NSDictionary和NSMutableDictionary

    NSDictionary    1.什么是字典        字典是也是一种集合结构,功能与我们现实中的字典工具一样    2.字典的元素是什么        任意类型的对象地址构成键值对    3. ...