-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.java
26 lines (24 loc) · 948 Bytes
/
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
class Solution {
public int maxMoves(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[][] dp = new int[m][n];
int maxMoves = 0;
for (int col = n - 2; col >= 0; col--) {
for (int row = 0; row < m; row++) {
if (row > 0 && grid[row][col] < grid[row - 1][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row - 1][col + 1] + 1);
}
if (grid[row][col] < grid[row][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row][col + 1] + 1);
}
if (row < m - 1 && grid[row][col] < grid[row + 1][col + 1]) {
dp[row][col] = Math.max(dp[row][col], dp[row + 1][col + 1] + 1);
}
}
}
for (int row = 0; row < m; row++) {
maxMoves = Math.max(maxMoves, dp[row][0]);
}
return maxMoves;
}
}