-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_329.java
44 lines (40 loc) · 1.42 KB
/
_329.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.fishercoder.solutions.firstthousand;
public class _329 {
public static class Solution1 {
final int[] dirs = new int[] {0, 1, 0, -1, 0};
public int longestIncreasingPath(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int max = 0;
int[][] cache = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int len = dfs(matrix, i, j, cache);
max = Math.max(len, max);
}
}
return max;
}
int dfs(int[][] matrix, int row, int col, int[][] cache) {
if (cache[row][col] != 0) {
return cache[row][col];
}
int max = 1;
for (int i = 0; i < dirs.length - 1; i++) {
int nextRow = row + dirs[i];
int nextCol = col + dirs[i + 1];
if (nextRow < 0
|| nextRow >= matrix.length
|| nextCol < 0
|| nextCol >= matrix[0].length
|| matrix[nextRow][nextCol] <= matrix[row][col]) {
continue;
}
int len = 1 + dfs(matrix, nextRow, nextCol, cache);
max = Math.max(max, len);
}
cache[row][col] = max;
return max;
}
}
}