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.

Constraints

  • rows == grid.length
  • cols == grid[i].length
  • 2 ≤ rows, cols ≤ 70
  • 0 ≤ grid[i][j] ≤ 100
  • Both robots must reach the bottom row

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.
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