-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path329-longest-increasing-path-in-a-matrix.java
40 lines (34 loc) · 1.36 KB
/
329-longest-increasing-path-in-a-matrix.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
class Solution {
int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public int longestIncreasingPath(int[][] matrix) {
// initialization
int maxLength = 0;
int[][] dp = new int[matrix.length][matrix[0].length];
// iterate over each cell in the matrix
for(int row = 0;row < matrix.length;row++){
for(int col = 0;col < matrix[0].length;col++){
// maximize the increasing length
maxLength = Math.max(maxLength, getLongestIncreasingPath(row, col, -1, matrix, dp));
}
}
return maxLength;
}
public int getLongestIncreasingPath(int row, int col, int prev, int[][] matrix, int[][] dp){
// base cases
if(row < 0 || row >= matrix.length || col < 0 || col >= matrix[0].length || matrix[row][col] <= prev){
return 0;
}
// if already visited
if(dp[row][col] != 0){
return dp[row][col];
}
int currMax = 0;
// for each directions maxmize the longest increasing length
for(int[] direction : directions){
currMax = Math.max(currMax, getLongestIncreasingPath(row + direction[0], col + direction[1], matrix[row][col], matrix, dp));
}
// store it adding extra 1 for the current cell
dp[row][col] = 1 + currMax;
return dp[row][col];
}
}