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. input 框 宽度100%时 padding 超出问题解决

    如下图: 让input  宽度100%, 加边框并有左填充,这里如果用 padding-left: 的话,input 边框会超出100%的范围 后来发现 text-indet: XXpx; 即可实现即 ...

  2. 开源项目管理平台*redmine*的架设

    yum -y install ruby yum install rubygems gem install heroku gem install rack -v=1.0.1 gem install ru ...

  3. phpStudy 创建多个站点,绑定域名

    默认情况下,phpStudy 的站点根目录是在它自己的WWW目录,比如 F:\phpStudy\WWW,访问的地址可以是 http://127.0.0.1/   或 http://localhost/ ...

  4. oracle 的wm_concat函数使用

    转载自:http://blog.itpub.net/post/42245/522757 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来 ...

  5. localdomain的linux域

    both和and的区别? both强调(两者)都,用于肯定语气, and字面意思是"和,而且",是用于表示并列关系的句子,通常位于最后两个人或物之间,将将人物串联起来 如: thi ...

  6. apt-get方式安装lnmp环境

    安装nginxsudo apt-get install nginx安装php和mysqlsudo apt-get install php5-cli php5-cgi php5-curl php5-my ...

  7. 服务器上的json类型的文件提示找不到

    搞Google地图的时候, 本地是可以显示的, 但是到了服务器上地图一直显示不出来, 火狐打开F12,发现是找不到json的文件, 本来还以为是IIS中"请求筛选"的问题,后来发现 ...

  8. mysql搜索引擎 小结

    mysql搜索引擎 小结 mysql5.5以后,mysql默认使用InnoDB存储引擎. 若要修改默认引擎,可以修改配置文件中的default-storage-engine.可以通过show vari ...

  9. 关于QString::toWCharArray 无法解析的外部符号

    1>CommManger.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall QString::toWCharArray(un ...

  10. 使用ajax获取JSON数据的jQuery代码的格式

    具体的也可以参考:http://www.w3cfuns.com/notes/16039/2b004b1bcdf79092f2e66b2bbe9f51df.html