DEV Community

Prashant Mishra
Prashant Mishra

Posted on • Originally published at leetcode.com

2 1

Cherry Pickup II Leetcode Problem

You are given a rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell.

You have two robots that can collect cherries for you:

  • Robot #1 is located at the top-left corner (0, 0), and
  • Robot #2 is located at the top-right corner (0, cols - 1).

Return the maximum number of cherries collection using both robots by following the rules below:

  • From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1).
  • When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
  • When both robots stay in the same cell, only one takes the cherries.
  • Both robots cannot move outside of the grid at any moment.
  • Both robots should reach the bottom row in grid.


Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: 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.

class Solution {
    // we will use 3dp for optimizing this problem
    public int cherryPickup(int[][] grid) {
        // since both the robots are moving simultaneously hence they will 
        // be at the same row all the time.
        // hence only one row index is needed .
        // j1 is for robot1 and j2 is for robot2
        int dp[][][] = new int[grid.length][grid[0].length][grid[0].length];
        for(int i =0;i<dp.length;i++){
            for(int matrix[] : dp[i]){
                Arrays.fill(matrix,-1);
            }
        }
        return solve(grid,0,0,grid[0].length-1,dp);
    }
    public int solve(int[][] grid, int i,int j1,int j2,int [][][] dp){
        if(j1<0 || j2<0 || j1>=grid[0].length || j2 >= grid[0].length) return 0;
        if(i == grid.length-1) {
            // here are two cases , either both robot1 and robot2 have arrived at the same cell in the last row or different cells in the last row
            if(j1==j2) return grid[i][j1];
            else return grid[i][j1] + grid[i][j2];
        }
        if(dp[i][j1][j2]!=-1) return dp[i][j1][j2];
        int maximum = 0;
        /*
        for every move of robot1 robot2 has the chance to move either rightdiagonal or down or left diagonal
        hence the below code will run for 9 times 
        3*3
        */
        for(int a = j1-1;a<=j1+1;a++){
            for(int b = j2-1;b<=j2+1;b++){
                if(j1==j2) { // since j1 and j2 are same that means robot1 and robot2 are at the same cell . Hence take current cell value + solve() again we can take index j1 or j2 any one as the value at the cell is same
                    maximum =Integer.max(maximum,grid[i][j1]+solve(grid,i+1,a,b,dp));
                }
                else {
                    maximum =Integer.max(maximum,  grid[i][j1]+grid[i][j2] + solve(grid,i+1,a,b,dp));
                }
            }
        }
        return dp[i][j1][j2] = maximum;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.