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 ...
随机推荐
- 表达式拼接Expression<Func<IEntityMapper, bool>> predicate
/// <summary> /// 重写以筛选出当前上下文的实体映射信息 /// </summary> protected override IEnumerable<IE ...
- Java备份约9亿条数据
需求:有一张表9亿多条数据,数据加索引总数据量61GB.考虑到这张表的大部分数据都不会再被使用并且大数据量可能影响整库的性能,所以决定将表里某一个时刻之前的数据备份到一张新表中,待备份完成后将旧表中已 ...
- Java面试笔记
1.&和&& if(str != null& !str.equals("")){ System.out.println("ok" ...
- 在ASP.MVC中使用Ajax
Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近.可以更自由.更灵活的去控制HTML的结构.样式和行为.Asp.net MVC可以更 ...
- 配置 nginx server 出现nginx: [emerg] "root" directive is duplicate in /etc/nginx/server/blogs.conf:7
在配置nginx 虚拟机时,执行 sudo /usr/sbin/nginx -t 报下面的错误: nginx: [emerg] nginx: configuration file /etc/nginx ...
- 论Linux运维的一些基础安全知识和简单办法
不知不觉本人来北京也已经第三个年头了,从一个Linux小小鸟,开始,2012年我参加了第一份工作,其实现在想想其实我是幸运的,本来求学的时候,就没好好的学Linux,我认为有Cisco知识从上wind ...
- Memcached目录
Memcached 简介.安装和基本使用 Memcached基础知识 理解Memcached的分布式 Memcached存储命令 - set Memcached存储命令 - add Memcached ...
- 微信或移动端网页的meta
针对微信: <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> ...
- 360随身WIFI程序单文件绿色版及网卡驱动(附使用感受)
大家好,我是Colin,今天刚收到传说中的360WIFI,拿到手后马上就进行了测试.就做工而言,19.9的价格算是比较公道的,网卡很小,做工还可以,带磨砂质感,而且还提供了一个耳机插头,可以当挂件一样 ...
- 格式化Double类型
//格式化Double类型 //F:默认是2位小数点 //F6:输出小数点后6位,不够的话用0补齐 //G:默认输出原先的,保留小数点后面的位数 LalTotal.Text = "合计:原始 ...