|
| 1 | +/* |
| 2 | +Chocolates Pickup |
| 3 | +Hard Accuracy: 54.34% Submissions: 9K+Points: 8 |
| 4 | +
|
| 5 | +You are given an r rows and c cols matrix grid representing a field of chocolates where grid[i][j] represents the number of chocolates that you can collect from the (i, j) cell. |
| 6 | +You have two robots that can collect chocolates for you: |
| 7 | +
|
| 8 | +Robot #1 is located at the top-left corner (0, 0), and |
| 9 | +Robot #2 is located at the top-right corner (0, cols - 1). |
| 10 | +Return the maximum number of chocolates collection using both robots by following the rules below: |
| 11 | +
|
| 12 | +From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). |
| 13 | +When any robot passes through a cell, It picks up all chocolates, and the cell becomes an empty cell. |
| 14 | +When both robots stay in the same cell, only one takes the chocolates. |
| 15 | +Both robots cannot move outside of the grid at any moment. |
| 16 | +Both robots should reach the bottom row in grid. |
| 17 | +Example: |
| 18 | +
|
| 19 | +Input: |
| 20 | +r = 3, c = 4 |
| 21 | +grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] |
| 22 | +Output: |
| 23 | +24 |
| 24 | +Explanation: |
| 25 | +Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. |
| 26 | +Your Task: |
| 27 | +You don't need to read input or print anything. Your task is to complete the function Solve() which takes r rows, c columns, and a matrix grid and returns the maximum number of chocolates that can be collected by two robots. |
| 28 | +
|
| 29 | +Expected Time Complexity: O(r * c * c) |
| 30 | +Expected Space Complexity: O(c * c * c) |
| 31 | +
|
| 32 | +Constraint: |
| 33 | +2 <= r < = 70 |
| 34 | +0 <= grid[i][j] <= 100 |
| 35 | +*/ |
| 36 | + |
| 37 | +class Solution{ |
| 38 | + static int[][][] memo; |
| 39 | + public int solve(int n, int m, int grid[][]){ |
| 40 | + // Code here |
| 41 | + memo = new int[n][m][m]; |
| 42 | + Arrays.stream(memo).forEach(plane -> Arrays.stream(plane).forEach(row -> Arrays.fill(row,-1))); |
| 43 | + //return solveRec(0,0,m-1,n,m,grid); |
| 44 | + |
| 45 | + //return solveTab(n,m,grid); |
| 46 | + return solveOpt(n,m,grid); |
| 47 | + } |
| 48 | + |
| 49 | + public int solveRec(int i,int j1,int j2,int n,int m,int[][] grid){ |
| 50 | + //Base Case :: |
| 51 | + if(j1<0 || j2<0 || j1>=m || j2>=m ){ |
| 52 | + return (int)-1e8; |
| 53 | + } |
| 54 | + |
| 55 | + if(memo[i][j1][j2]!=-1){ |
| 56 | + return memo[i][j1][j2]; |
| 57 | + } |
| 58 | + if(i == n-1 ){ |
| 59 | + if(j1==j2){ |
| 60 | + return grid[i][j1]; |
| 61 | + }else{ |
| 62 | + return grid[i][j1]+ grid[i][j2]; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + // 3 - state - > 3 state --> 9 state |
| 68 | + int maxi = (int)-1e8; |
| 69 | + for(int dj1=-1;dj1<2;dj1++){ |
| 70 | + for(int dj2=-1;dj2<2;dj2++){ |
| 71 | + int val =0; |
| 72 | + // |
| 73 | + if(j1 == j2 ){ |
| 74 | + val = grid[i][j1]; |
| 75 | + }else{ |
| 76 | + val = grid[i][j1] + grid[i][j2]; |
| 77 | + } |
| 78 | + |
| 79 | + val += solveRec(i+1,j1+dj1,j2+dj2,n,m,grid); |
| 80 | + |
| 81 | + maxi = Math.max(maxi,val); |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + return memo[i][j1][j2] =maxi ; |
| 86 | + } |
| 87 | + public int solveTab(int n,int m,int[][] grid){ |
| 88 | + int[][][] tab = new int[n][m][m] ; |
| 89 | + |
| 90 | + //Base Case ::- |
| 91 | + for(int j1 =0;j1<m;j1++){ |
| 92 | + for(int j2=0;j2<m;j2++){ |
| 93 | + if(j1 == j2 ) { |
| 94 | + tab[n-1][j1][j2] = grid[n-1][j1]; |
| 95 | + }else{ |
| 96 | + tab[n-1][j1][j2] = grid[n-1][j1] + grid[n-1][j2]; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + // |
| 103 | + |
| 104 | + for(int i=n-2;i>=0 ;i--){ |
| 105 | + for(int j1 =0;j1<m;j1++){ |
| 106 | + for(int j2=0;j2<m;j2++){ |
| 107 | + int maxi = (int)-1e8; |
| 108 | + for(int dj1=-1;dj1<2;dj1++){ |
| 109 | + for(int dj2=-1;dj2<2;dj2++){ |
| 110 | + int val =0; |
| 111 | + // |
| 112 | + if(j1 == j2 ){ |
| 113 | + val = grid[i][j1]; |
| 114 | + }else{ |
| 115 | + val = grid[i][j1] + grid[i][j2]; |
| 116 | + } |
| 117 | + |
| 118 | + if(j1+dj1>=0 && j1+dj1<m && j2+dj2>=0 && j2+dj2<m){ |
| 119 | + val += tab[i+1][j1+dj1][j2+dj2]; |
| 120 | + }else{ |
| 121 | + val += (int)-1e8; |
| 122 | + } |
| 123 | + |
| 124 | + maxi = Math.max(maxi,val); |
| 125 | + } |
| 126 | + } |
| 127 | + tab[i][j1][j2] = maxi ; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return tab[0][0][m-1]; |
| 132 | + } |
| 133 | + public int solveOpt(int n,int m,int[][] grid){ |
| 134 | + int[][] next = new int[m][m]; |
| 135 | + int[][] curr = new int[m][m]; |
| 136 | + |
| 137 | + //Base Case ::- |
| 138 | + for(int j1 =0;j1<m;j1++){ |
| 139 | + for(int j2=0;j2<m;j2++){ |
| 140 | + if(j1 == j2 ) { |
| 141 | + next[j1][j2] = grid[n-1][j1]; |
| 142 | + }else{ |
| 143 | + next[j1][j2] = grid[n-1][j1] + grid[n-1][j2]; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + // |
| 150 | + |
| 151 | + for(int i=n-2;i>=0 ;i--){ |
| 152 | + for(int j1 =0;j1<m;j1++){ |
| 153 | + for(int j2=0;j2<m;j2++){ |
| 154 | + int maxi = (int)-1e8; |
| 155 | + for(int dj1=-1;dj1<2;dj1++){ |
| 156 | + for(int dj2=-1;dj2<2;dj2++){ |
| 157 | + int val =0; |
| 158 | + // |
| 159 | + if(j1 == j2 ){ |
| 160 | + val = grid[i][j1]; |
| 161 | + }else{ |
| 162 | + val = grid[i][j1] + grid[i][j2]; |
| 163 | + } |
| 164 | + |
| 165 | + if(j1+dj1>=0 && j1+dj1<m && j2+dj2>=0 && j2+dj2<m){ |
| 166 | + val += next[j1+dj1][j2+dj2]; |
| 167 | + }else{ |
| 168 | + val += (int)-1e8; |
| 169 | + } |
| 170 | + |
| 171 | + maxi = Math.max(maxi,val); |
| 172 | + } |
| 173 | + } |
| 174 | + curr[j1][j2] = maxi ; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + for(int j1 =0;j1<m;j1++){ |
| 179 | + for(int j2=0;j2<m;j2++){ |
| 180 | + next[j1][j2] = curr[j1][j2]; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + return next[0][m-1]; |
| 186 | + } |
| 187 | +} |
0 commit comments