-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path329_Longest_Increasing_Path_in_a_Matrix.java
More file actions
64 lines (57 loc) · 1.82 KB
/
329_Longest_Increasing_Path_in_a_Matrix.java
File metadata and controls
64 lines (57 loc) · 1.82 KB
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
59
60
61
62
63
64
class Solution {
public int longestIncreasingPath(int[][] matrix) {
int[][] dp = new int[matrix.length][matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
dp[i][j] = -1;
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(dp[i][j] != -1) {
continue;
}
dp[i][j] = dfs(matrix,dp,new Point(i,j));
}
}
int answer = 0;
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
answer = dp[i][j] > answer ? dp[i][j] : answer;
}
}
return answer;
}
int dfs(int[][] m,int[][] dp,Point p) {
List<Point> access = new ArrayList<>();
if(p.x-1>=0 && m[p.x][p.y] < m[p.x-1][p.y]) {
access.add(new Point(p.x-1,p.y));
}
if(p.x+1<dp.length && m[p.x][p.y] < m[p.x+1][p.y]) {
access.add(new Point(p.x+1,p.y));
}
if(p.y-1>=0 && m[p.x][p.y] < m[p.x][p.y-1]) {
access.add(new Point(p.x,p.y-1));
}
if(p.y+1<dp[0].length && m[p.x][p.y] < m[p.x][p.y+1]) {
access.add(new Point(p.x,p.y+1));
}
if(access.isEmpty()){
return 1;
}
int longest = 0;
for(Point ac : access) {
dp[ac.x][ac.y] = dp[ac.x][ac.y] != -1 ? dp[ac.x][ac.y] : dfs(m,dp,new Point(ac.x,ac.y));
longest = longest > dp[ac.x][ac.y] ? longest : dp[ac.x][ac.y];
}
return longest + 1;
}
class Point {
int x;
int y;
Point(int x,int y) {
this.x = x;
this.y = y;
}
}
}