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
R1R2311251πŸ’ Cherry Collection Strategy:β€’ Both robots move down simultaneouslyβ€’ Each robot has 3 movement optionsβ€’ DP stores optimal results for each stateβ€’ Avoid double-counting shared cells
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)

n
2n
βœ“ Linear Growth
Space Complexity
O(rows Γ— colsΒ²)

3D DP table stores one value per (row, col1, col2) combination

n
2n
βœ“ 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
Asked in
Google 45 Amazon 38 Facebook 31 Microsoft 22
89.0K Views
Medium Frequency
~25 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen