-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathLongestIncreasingPathInAMatrix.java
175 lines (157 loc) · 5.04 KB
/
LongestIncreasingPathInAMatrix.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package LeetCodeJava.DynamicProgramming;
// https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
import java.util.LinkedList;
import java.util.Queue;
/**
* 329. Longest Increasing Path in a Matrix
* Hard
* Topics
* Companies
* Given an m x n integers matrix, return the length of the longest increasing path in matrix.
*
* From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
*
*
*
* Example 1:
*
*
* Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
* Output: 4
* Explanation: The longest increasing path is [1, 2, 6, 9].
* Example 2:
*
*
* Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
* Output: 4
* Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
* Example 3:
*
* Input: matrix = [[1]]
* Output: 1
*
*
* Constraints:
*
* m == matrix.length
* n == matrix[i].length
* 1 <= m, n <= 200
* 0 <= matrix[i][j] <= 231 - 1
*
*
*/
public class LongestIncreasingPathInAMatrix {
// V0
// public int longestIncreasingPath(int[][] matrix) {
//
// }
// V1-1
// https://neetcode.io/problems/longest-increasing-path-in-matrix
// IDEA: Recursion
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
private int dfs(int[][] matrix, int r, int c, int prevVal) {
int ROWS = matrix.length, COLS = matrix[0].length;
if (r < 0 || r >= ROWS || c < 0 ||
c >= COLS || matrix[r][c] <= prevVal) {
return 0;
}
int res = 1;
for (int[] d : directions) {
res = Math.max(res, 1 + dfs(matrix, r + d[0],
c + d[1], matrix[r][c]));
}
return res;
}
public int longestIncreasingPath_1_1(int[][] matrix) {
int ROWS = matrix.length, COLS = matrix[0].length;
int LIP = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
LIP = Math.max(LIP, dfs(matrix, r, c, Integer.MIN_VALUE));
}
}
return LIP;
}
// V1-2
// https://neetcode.io/problems/longest-increasing-path-in-matrix
// IDEA: Dynamic Programming (Top-Down)
//int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int[][] dp;
private int dfs_1_2(int[][] matrix, int r, int c, int prevVal) {
int ROWS = matrix.length, COLS = matrix[0].length;
if (r < 0 || r >= ROWS || c < 0 ||
c >= COLS || matrix[r][c] <= prevVal) {
return 0;
}
if (dp[r][c] != -1) return dp[r][c];
int res = 1;
for (int[] d : directions) {
res = Math.max(res, 1 + dfs_1_2(matrix, r + d[0],
c + d[1], matrix[r][c]));
}
return dp[r][c] = res;
}
public int longestIncreasingPath_1_2(int[][] matrix) {
int ROWS = matrix.length, COLS = matrix[0].length;
int LIP = 0;
dp = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
dp[i][j] = -1;
}
}
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
LIP = Math.max(LIP, dfs_1_2(matrix, r, c, Integer.MIN_VALUE));
}
}
return LIP;
}
// V1-3
// https://neetcode.io/problems/longest-increasing-path-in-matrix
// IDEA: Topological Sort (Kahn's Algorithm)
public int longestIncreasingPath_1_3(int[][] matrix) {
int ROWS = matrix.length, COLS = matrix[0].length;
int[][] indegree = new int[ROWS][COLS];
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int r = 0; r < ROWS; ++r) {
for (int c = 0; c < COLS; ++c) {
for (int[] d : directions) {
int nr = r + d[0], nc = c + d[1];
if (nr >= 0 && nr < ROWS && nc >= 0 &&
nc < COLS && matrix[nr][nc] < matrix[r][c]) {
indegree[r][c]++;
}
}
}
}
Queue<int[]> q = new LinkedList<>();
for (int r = 0; r < ROWS; ++r) {
for (int c = 0; c < COLS; ++c) {
if (indegree[r][c] == 0) {
q.offer(new int[]{r, c});
}
}
}
int LIS = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
int[] node = q.poll();
int r = node[0], c = node[1];
for (int[] d : directions) {
int nr = r + d[0], nc = c + d[1];
if (nr >= 0 && nr < ROWS && nc >= 0 &&
nc < COLS && matrix[nr][nc] > matrix[r][c]) {
if (--indegree[nr][nc] == 0) {
q.offer(new int[]{nr, nc});
}
}
}
}
LIS++;
}
return LIS;
}
// V2
}