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:
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的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(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 ...
- 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 Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer 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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- SSLv3 Poodle攻击漏洞检测工具
漏洞编号:CVE-2014-3566 POC如下: import ssl,socket,sys SSL_VERSION={ 'SSLv2':ssl.PROTOCOL_SSLv2, 'SSL ...
- BufferedReader readLine()方法
控制台输入字符串之后回车,后台接收传来的字符串,代码如下: import java.io.BufferedReader; import java.io.IOException; import java ...
- 再谈 X-UA-Compatible 兼容模式
如何理解 IE 的文档兼容模式(X-UA-Compatible)? IE 浏览器支持多种文档兼容模式,得以因此改变页面的渲染效果. IE9 模式支持全范围的既定行业标准,包括 HTML5(草案), W ...
- (2)apply函数及其源码
本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...
- 安装cocopods
http://www.tuicool.com/articles/7VvuAr3 OS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove h ...
- ScriptManager与UpdatePanel总结
1.From http://www.cnblogs.com/Tim-Seven/archive/2011/02/11/1952409.html Ajax Extensions 2.ScriptMana ...
- isNaN() 确认是否是数字
isNaN(x): 当变量 x 不是数字,返回 true: 当变量 x 是其他值,(比如,1,2,3),返回false.
- DAY3 python群发短信
手机轰炸,burpsuit 抓取注册页面输入的手机号,然后每点击一次forword ,都开开始放行,发短信.也可以发到repeat 里面进行 ,重复发送短信. import requests impo ...
- 以下css可以清除浮动
.clearfix:after { content: ""; clear: both; visibility: hidden; display: block; height: 0p ...
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...