-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestIncreasingPathInAMatrix.java
73 lines (62 loc) · 1.69 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
package matrix;
/**
* @author Shogo Akiyama
* Solved on 12/06/2019
*
* 329. Longest Increasing Path in a Matrix
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
* Difficulty: Hard
*
* Approach: DFS & Recursion
* Runtime: 8 ms, faster than 72.67% of Java online submissions for Longest Increasing Path in a Matrix.
* Memory Usage: 39.7 MB, less than 97.96% of Java online submissions for Longest Increasing Path in a Matrix.
*
* Time Complexity: O(m*n)
* Space Complexity: O(m*n)
* Where m * n is the number of elements in a matrix
*
* @see MatrixTest#testLongestIncreasingPathInAMatrix()
*/
public class LongestIncreasingPathInAMatrix {
private int m;
private int n;
private int[][] grid;
private int[][] seen;
private int[][] directions;
private int max;
public int longestIncreasingPath(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
m = matrix.length;
n = matrix[0].length;
grid = matrix;
seen = new int[m][n];
directions = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
max = Math.max(max, dfs(i, j, 0));
}
}
return max;
}
private int dfs(int y, int x, int curr) {
if (seen[y][x] != 0) {
return curr + seen[y][x];
}
int val = grid[y][x];
grid[y][x] = Integer.MIN_VALUE;
int currMax = curr + 1;
for (int[] next : directions) {
int ny = y + next[0];
int nx = x + next[1];
if (ny >= 0 && ny < m && nx >= 0 && nx < n && grid[ny][nx] > val) {
currMax = Math.max(currMax, dfs(ny, nx, curr + 1));
}
}
grid[y][x] = val;
seen[y][x] = Math.max(1, currMax - curr);
return currMax;
}
}