Advertisement

Cherry Pickup II - LeetCode 1463 Solution

Cherry Pickup II - Complete Solution Guide

Cherry Pickup II is LeetCode problem 1463, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the maximum number of cherries two robots can collect in a grid. Robot 1 starts at the top-left corner (0, 0) and Robot 2 starts at the top-right corner (0, cols - 1). Both robots move downwards, one row at a time. From cell (i, j), each robot can move to (i+1, j-1), (i+1, j), or (i+1, j+1). When a robot passes through a cell, it picks up all the cherries, and the cell becomes empty. If both robots are in the same cell, only one of them picks up the cherries. The goal is to maximize the total cherries collected when both robots reach the bottom row.

Solution Approach

The provided code uses a dynamic programming approach. It iterates through each row of the grid, maintaining a 2D DP table `dp` where `dp[c1][c2]` represents the maximum number of cherries that can be collected up to the current row, with Robot 1 at column `c1` and Robot 2 at column `c2`. For each cell in the `dp` table, it considers all possible moves the robots could have made from the previous row and calculates the cherries collected in the current cell, updating the `dp` table accordingly. A temporary `next_dp` table is used to store intermediate values before updating the main `dp` table at the end of each row processing.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D DP table `dp` of size `cols x cols` with all values set to -1. Set the initial state `dp[0][cols - 1]` to the sum of cherries at (0, 0) and (0, cols - 1) (or just grid[0][0] if cols == 1).
  2. Step 2: Iterate through the rows of the grid from the second row (r = 1) to the last row.
  3. Step 3: For each row, create a temporary DP table `next_dp` to store intermediate results.
  4. Step 4: Iterate through all possible column positions `c1` and `c2` for Robot 1 and Robot 2, respectively.
  5. Step 5: If `dp[c1][c2]` is -1, it means this state is unreachable from the previous row, so skip to the next iteration.
  6. Step 6: For each possible move `d1` and `d2` (-1, 0, or 1) for Robot 1 and Robot 2, calculate the next column positions `nc1 = c1 + d1` and `nc2 = c2 + d2`.
  7. Step 7: Check if `nc1` and `nc2` are within the grid boundaries (0 <= `nc1` < cols and 0 <= `nc2` < cols).
  8. Step 8: If the next positions are valid, calculate the cherries collected in the current row. If `nc1` and `nc2` are the same, the robots are in the same cell, so collect only `grid[r + 1][nc1]`. Otherwise, collect `grid[r + 1][nc1] + grid[r + 1][nc2]`.
  9. Step 9: Update `next_dp[nc1][nc2]` with the maximum of its current value and `dp[c1][c2] + cherries`.
  10. Step 10: After processing all possible moves for the current row, update `dp` with `next_dp`.
  11. Step 11: After processing all rows, find the maximum value in the final `dp` table, which represents the maximum number of cherries that can be collected.

Key Insights

  • Insight 1: Dynamic Programming is suitable because we can break down the problem into subproblems: finding the maximum cherries collected up to a certain row for all possible positions of the two robots.
  • Insight 2: We need to maintain a state that captures the current row and the column positions of both robots.
  • Insight 3: The transition function involves considering all possible moves for both robots from their current positions to the next row, ensuring they stay within the grid boundaries.

Complexity Analysis

Time Complexity: O(m*n^2)

Space Complexity: O(n^2)

Topics

This problem involves: Array, Dynamic Programming, Matrix.

Companies

Asked at: Flipkart, Rubrik.