-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCherry-Pickup-II.java
45 lines (39 loc) · 1.24 KB
/
Cherry-Pickup-II.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
class Solution {
int[][][] dp;
public int cherryPickup(int[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
dp = new int[rows][cols][cols];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
for(int k = 0; k < cols; k++){
dp[i][j][k] = -1;
}
}
}
return helper(grid, 0, 0, cols - 1);
}
private int helper(int[][] grid, int row, int col1, int col2){
if(col1 < 0 || col1 >= grid[0].length || col2 < 0 || col2 >= grid[0].length){
return 0;
}
if(dp[row][col1][col2] != -1){
return dp[row][col1][col2];
}
int count = 0;
count += grid[row][col1];
if(col1 != col2){
count += grid[row][col2];
}
if(row != grid.length - 1){
int max = 0;
for(int newCol1 = col1 - 1; newCol1 <= col1 + 1; newCol1++){
for(int newCol2 = col2 - 1; newCol2 <= col2 + 1; newCol2++){
max = Math.max(max, helper(grid, row + 1, newCol1, newCol2));
}
}
count += max;
}
return dp[row][col1][col2] =count;
}
}