|
| 1 | +// Runtime: 12 ms (Top 87.2%) | Memory: 44.42 MB (Top 46.1%) |
| 2 | + |
1 | 3 | class Solution {
|
2 |
| - int max = 0; |
3 | 4 | public int cherryPickup(int[][] grid) {
|
4 |
| - int m = grid.length; |
5 |
| - int n = grid[0].length; |
6 |
| - if(m == 1 && n == 1) |
7 |
| - return grid[0][0]; |
8 |
| - dfs(0, 0, m, n, grid, 0); |
9 |
| - return max; |
| 5 | + int m = grid.length, n = grid[0].length; |
| 6 | + //For O(N^3) Dp sapce solution |
| 7 | + dp2 = new Integer[m][n][m]; |
| 8 | + int ans=solve2(0,0,0,grid,0,m,n); |
| 9 | + if(ans==Integer.MIN_VALUE) return 0; |
| 10 | + return ans; |
10 | 11 | }
|
11 |
| - public void dfs(int i, int j, int m, int n, int[][] grid, int ccsf){ |
12 |
| - if(i<0 || i>=m || j<0 || j>=n || grid[i][j] == -1) |
13 |
| - return; |
14 |
| - if(i == m-1 && j == n-1){ |
15 |
| - helper(i, j, m, n, grid, ccsf); |
| 12 | + |
| 13 | + private Integer[][][] dp2; |
| 14 | + private int solve2(int x1, int y1, int x2, int[][] g, int cpsf, int m, int n){ |
| 15 | + int y2 = x1+y1+-x2; |
| 16 | + if(x1>=m||x2>=m||y1>=n||y2>=n||g[x1][y1]==-1||g[x2][y2]==-1) return Integer.MIN_VALUE; |
| 17 | + if(x1==m-1&&y1==n-1) return g[x1][y1]; |
| 18 | + //If both p1 and p2 reach (m-1,n-1) |
| 19 | + if(dp2[x1][y1][x2]!=null) return dp2[x1][y1][x2]; |
| 20 | + int cherries=0; |
| 21 | + //If both p1 and p2 are at same position then we need to add the cherry only once. |
| 22 | + if(x1==x2&&y1==y2){ |
| 23 | + cherries+=g[x1][y1]; |
16 | 24 | }
|
17 |
| - int cheeries = grid[i][j]; |
18 |
| - grid[i][j] = 0; |
19 |
| - dfs(i+1, j, m, n, grid, ccsf+cheeries); |
20 |
| - dfs(i, j+1, m, n, grid, ccsf+cheeries); |
21 |
| - grid[i][j] = cheeries; |
22 |
| - } |
23 |
| - public void helper(int i, int j, int m, int n, int[][] grid, int ccsf){ |
24 |
| - if(i<0 || i>=m || j<0 || j>=n || grid[i][j] == -1) |
25 |
| - return; |
26 |
| - if(i == 0 && j == 0){ |
27 |
| - max = Math.max(max, ccsf); |
28 |
| - return; |
| 25 | + //If p1 and p2 are at different positions then repective cherries can be added. |
| 26 | + else{ |
| 27 | + cherries+=g[x1][y1]+g[x2][y2]; |
29 | 28 | }
|
30 |
| - int cheeries = grid[i][j]; |
31 |
| - grid[i][j] = 0; |
32 |
| - helper(i-1, j, m, n, grid, ccsf+cheeries); |
33 |
| - helper(i, j-1, m, n, grid, ccsf+cheeries); |
34 |
| - grid[i][j] = cheeries; |
| 29 | + //4 possibilites for p1 and p2 from each point |
| 30 | + int dd=solve2(x1+1,y1,x2+1,g,cpsf+cherries,m,n); //both moves down |
| 31 | + int dr=solve2(x1+1,y1,x2,g,cpsf+cherries,m,n); //p1 moves down and p2 moves right |
| 32 | + int rr=solve2(x1,y1+1,x2,g,cpsf+cherries,m,n); //both moves right |
| 33 | + int rd=solve2(x1,y1+1,x2+1,g,cpsf+cherries,m,n); //p1 moves right and p2 moves down |
| 34 | + |
| 35 | + //We take maximum of 4 possiblities |
| 36 | + int max=Math.max(Math.max(dd,dr), Math.max(rr,rd)); |
| 37 | + if(max==Integer.MIN_VALUE) return dp2[x1][y1][x2]=max; |
| 38 | + return dp2[x1][y1][x2]=cherries+=max; |
35 | 39 | }
|
36 | 40 | }
|
0 commit comments