forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestIncreasingPath.java
58 lines (49 loc) · 1.45 KB
/
LongestIncreasingPath.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
// Tc : O(n*m)
// SC : O(m*n)
private int[][] directions = {{1,0},{0,1},{-1,0},{0,-1}};
public int longestIncreasingPath(int[][] matrix) {
if(matrix == null){
return 0;
}
int m = matrix.length;
if(m==0){
return 0;
}
int n = matrix[0].length;
if(n ==0){
return 0;
}
int[][] cache = new int[m][n];
int maxLength = 1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int currentMaxLength = dfs(i,j, matrix,m,n,cache);
maxLength =Math.max(maxLength, currentMaxLength);
}
}
return maxLength;
}
private int dfs(int i, int j, int[][] matrix, int m, int n, int[][] cache){
// if(i<0 || j<0 || i>=m || j>=n){
// return 0;
// }
if(cache[i][j]!=0){
return cache[i][j];
}
int currentLen = 1;
for(int[] direction : directions){
int neighX = i + direction[0];
int neighY = j + direction[1];
if(neighX<0||neighY<0 || neighX >=m ||neighY>=n){
continue;
}
if(matrix[i][j] >= matrix[neighX][neighY]){
continue;
}
currentLen = Math.max(currentLen, 1+ dfs(neighX, neighY, matrix, m,n, cache));
}
cache[i][j] = currentLen;
return currentLen;
}
}