Cherry Pickup II - Problem
Imagine you're managing a cherry orchard with two advanced harvesting robots! π€π
You have a rows Γ cols grid representing a cherry field where grid[i][j] contains the number of cherries at position (i, j).
The Setup:
- π€ Robot #1 starts at the top-left corner
(0, 0) - π€ Robot #2 starts at the top-right corner
(0, cols-1)
Movement Rules:
- From cell
(i, j), robots can move to:(i+1, j-1),(i+1, j), or(i+1, j+1) - When a robot passes through a cell, it collects all cherries and the cell becomes empty
- If both robots visit the same cell, only one robot gets the cherries
- Both robots must reach the bottom row
Goal: Find the maximum number of cherries both robots can collect together!
Input & Output
example_1.py β Basic Case
$
Input:
grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
βΊ
Output:
24
π‘ Note:
Robot 1: (0,0)β(1,1)β(2,1)β(3,1) collecting 3+5+5+1=14 cherries. Robot 2: (0,2)β(1,1)β(2,2)β(3,1) but cell (1,1) already visited by Robot 1, so collects 1+0+5+0=6 cherries. Wait, that's wrong! Both robots move simultaneously, so at (1,1) they collect 5 cherries once. Optimal path: Robot1: (0,0)β(1,0)β(2,1)β(3,1) = 3+2+5+1=11. Robot2: (0,2)β(1,1)β(2,2)β(3,1) = 1+5+5+0=11 (but (3,1) shared). Total: 3+2+1+5+5+5+1 = 22. Actually, optimal is 24 with different paths.
example_2.py β Small Grid
$
Input:
grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
βΊ
Output:
28
π‘ Note:
Robots start at (0,0) and (0,6). They need to collect maximum cherries while moving down. The large 9 in the middle and strategic positioning allows for optimal collection of 28 cherries total.
example_3.py β Edge Case - Single Row
$
Input:
grid = [[1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1]]
βΊ
Output:
2
π‘ Note:
With only one row, Robot 1 starts at position 0 (collects 1 cherry) and Robot 2 starts at position 19 (collects 1 cherry). Total = 2 cherries since they can't move anywhere.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Robot 1 at top-left (0,0), Robot 2 at top-right (0,cols-1)
2
Simultaneous Movement
Both robots move down simultaneously, each with 3 possible directions
3
Cherry Collection
Collect cherries from visited cells, avoid double-counting when both visit same cell
4
Dynamic Programming
Use DP to store optimal results for each state (row, col1, col2)
Key Takeaway
π― Key Insight: Use 3D DP with state (row, col1, col2) to efficiently compute the maximum cherries collectible by both robots working together, reducing complexity from exponential to polynomial.
Time & Space Complexity
Time Complexity
O(rows Γ colsΒ²)
For each of rowsΓcolsΒ² states, we do constant work (9 transitions)
β Linear Growth
Space Complexity
O(rows Γ colsΒ²)
3D DP table stores one value per (row, col1, col2) combination
β Linear Space
Constraints
- rows == grid.length
- cols == grid[i].length
- 2 β€ rows, cols β€ 70
- 0 β€ grid[i][j] β€ 100
- Both robots must reach the bottom row
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code