In Cherry Pickup II, you have two robots starting at the top-left and top-right of a grid. Both robots move down to the bottom row. In each step, a robot can move to the cell below, below-left, or below-right. If both robots land in the same cell, only one collects the cherries. The goal is to maximize the total cherries collected by both robots.
Companies like Uber and Google use this to test your ability to handle 3D Dynamic Programming. Unlike the first Cherry Pickup, the robots move strictly downwards, which simplifies the state but still requires you to manage the interaction between two paths. It evaluations your skill in state transitions and handling boundary conditions.
This is a 3D Dynamic Programming problem. The state is dp[row][col1][col2], representing the maximum cherries collected when Robot 1 is at (row, col1) and Robot 2 is at (row, col2). Since both robots move down one row in every step, we only need to track the current row. For each state, there are possible previous states to consider (each robot could have come from 3 positions).
Row 0: Robot 1 at (0,0), Robot 2 at (0, 6) in a grid. Row 1:
A common mistake is not handling the "same cell" condition correctly—Robot 1 and Robot 2 should not both add the cherries if col1 == col2. Another error is not properly checking the grid boundaries for the diagonal moves (below-left and below-right). Using a 3D array for DP is fine, but you can optimize it to use only two 2D arrays (current row and previous row) to save space.
Practice visualizing the state transitions. In 3D DP, it's helpful to think: "To be in this state at Row , where could I have been at Row ?". This bottom-up thinking is the core of dynamic programming.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Dungeon Game | Hard | Solve | |
| Minimum Falling Path Sum II | Hard | Solve | |
| Cherry Pickup | Hard | Solve | |
| Maximum Vacation Days | Hard | Solve | |
| Check if There Is a Valid Parentheses String Path | Hard | Solve |