-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
274 lines (245 loc) · 8.56 KB
/
Solution.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package L329;
import java.util.*;
class Solution2 {
private int m, n;
private int[] dx = {-1, 0, 0, 1};
private int[] dy = {0, -1, 1, 0};
private int[][] dist;
private int[][] matrix;
/**
* 记忆化搜索
*
* @param matrix
* @return
*/
public int longestIncreasingPath(int[][] matrix) {
this.m = matrix.length;
this.n = matrix[0].length;
this.dist = new int[m][n];
this.matrix = matrix;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
}
private int dfs(int x, int y) {
if (dist[x][y] != 0) return dist[x][y];
dist[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (valid(nx, ny) && matrix[nx][ny] > matrix[x][y]) {
dist[x][y] = Math.max(dist[x][y], dfs(nx, ny) + 1);
}
}
return dist[x][y];
}
private boolean valid(int i, int j) {
return i >= 0 && i < m && j >= 0 && j < n;
}
}
public class Solution {
private int m, n;
private int[] deg, dist;
private Set<Integer>[] to;
private int[] dx = {-1, 0, 0, 1};
private int[] dy = {0, -1, 1, 0};
private void addEdge(int u, int v) {
deg[v]++;
if (to[u] == null) to[u] = new HashSet<>();
to[u].add(v);
}
private int num(int i, int j) {
return i * n + j;
}
private boolean valid(int i, int j) {
return i >= 0 && i < m && j >= 0 && j < n;
}
public int longestIncreasingPath(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
this.m = matrix.length;
this.n = matrix[0].length;
this.to = new HashSet[m * n];
this.deg = new int[m * n];
this.dist = new int[m * n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (valid(ni, nj) && matrix[ni][nj] > matrix[i][j]) {
addEdge(num(i, j), num(ni, nj));
}
}
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < m * n; ++i) {
if (deg[i] == 0) {
queue.add(i);
dist[i] = 1;
}
}
while (!queue.isEmpty()) {
int x = queue.poll();
if (to[x] != null) {
for (int y : to[x]) {
deg[y]--;
dist[y] = Math.max(dist[y], dist[x] + 1);
if (deg[y] == 0) queue.add(y);
}
}
}
int ans = 0;
for (int i = 0; i < m * n; ++i) {
ans = Math.max(ans, dist[i]);
}
return ans;
}
public int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int rows, columns;
/**
* 官方解法:记忆化深度优先搜索
*
* @param matrix
* @return
*/
public int longestIncreasingPath2(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
rows = matrix.length;
columns = matrix[0].length;
int[][] memo = new int[rows][columns];
int ans = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
ans = Math.max(ans, dfs(matrix, i, j, memo));
}
}
return ans;
}
public int dfs(int[][] matrix, int row, int column, int[][] memo) {
if (memo[row][column] != 0) {
return memo[row][column];
}
++memo[row][column];
for (int[] dir : dirs) {
int newRow = row + dir[0], newColumn = column + dir[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[row][column]) {
memo[row][column] = Math.max(memo[row][column], dfs(matrix, newRow, newColumn, memo) + 1);
}
}
return memo[row][column];
}
/**
* 拓扑排序
*
* @param matrix
* @return
*/
public int longestIncreasingPath3(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
rows = matrix.length;
columns = matrix[0].length;
int[][] outdegrees = new int[rows][columns];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
for (int[] dir : dirs) {
int newRow = i + dir[0], newColumn = j + dir[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[i][j]) {
++outdegrees[i][j];
}
}
}
}
Queue<int[]> queue = new LinkedList<int[]>();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
if (outdegrees[i][j] == 0) {
queue.offer(new int[]{i, j});
}
}
}
int ans = 0;
while (!queue.isEmpty()) {
++ans;
int size = queue.size();
for (int i = 0; i < size; ++i) {
int[] cell = queue.poll();
int row = cell[0], column = cell[1];
for (int[] dir : dirs) {
int newRow = row + dir[0], newColumn = column + dir[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] < matrix[row][column]) {
--outdegrees[newRow][newColumn];
if (outdegrees[newRow][newColumn] == 0) {
queue.offer(new int[]{newRow, newColumn});
}
}
}
}
}
return ans;
}
private int[][] direction = new int[][]{{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
public int longestIncreasingPath4(int[][] matrix) {
if (matrix == null || matrix.length == 0)
return 0;
m = matrix.length;
n = matrix[0].length;
int maxLen = 0;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
maxLen = Math.max(maxLen, dfs(matrix, dp, i, j));
}
}
return maxLen;
}
private int dfs(int[][] matrix, int[][] dp, int r, int c) {
if (dp[r][c] != 0)
return dp[r][c];
for (int[] d : direction) {
if (valid(r + d[0], c + d[1]) && matrix[r][c] < matrix[r + d[0]][c + d[1]]) {
dp[r][c] = Math.max(dp[r][c], dfs(matrix, dp, r + d[0], c + d[1]));
}
}
return ++dp[r][c];
}
public static void main(String[] args) {
Solution solution = new Solution();
Solution2 solution2 = new Solution2();
int[][] matrix = {{9, 9, 4},
{6, 6, 8},
{2, 1, 1}};
int ans = solution.longestIncreasingPath(matrix);
int ans2 = solution.longestIncreasingPath2(matrix);
int ans3 = solution.longestIncreasingPath3(matrix);
int ans4 = solution.longestIncreasingPath4(matrix);
int ans5 = solution2.longestIncreasingPath(matrix);
System.out.println(ans + "-----" + ans2 + "-----" + ans3 + "-----" + ans4 + "-----" + ans5);
int[][] matrix2 = {{3, 4, 5},
{3, 2, 6},
{2, 2, 1}};
ans = solution.longestIncreasingPath(matrix2);
ans2 = solution.longestIncreasingPath2(matrix2);
ans3 = solution.longestIncreasingPath3(matrix2);
ans4 = solution.longestIncreasingPath4(matrix2);
ans5 = solution2.longestIncreasingPath(matrix2);
System.out.println(ans + "-----" + ans2 + "-----" + ans3 + "-----" + ans4 + "-----" + ans5);
int[][] matrix3 = {{1}};
ans = solution.longestIncreasingPath(matrix3);
ans2 = solution.longestIncreasingPath2(matrix3);
ans3 = solution.longestIncreasingPath3(matrix3);
ans4 = solution.longestIncreasingPath4(matrix3);
ans5 = solution2.longestIncreasingPath(matrix3);
System.out.println(ans + "-----" + ans2 + "-----" + ans3 + "-----" + ans4 + "-----" + ans5);
}
}