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
Cherry Pickup II - Dynamic Programming INPUT Cherry Field (4x3 grid) 3 1 1 2 5 1 1 5 5 2 1 1 R1 R2 grid = [[3,1,1], [2,5,1], [1,5,5], [2,1,1]] R1: (0,0) | R2: (0,2) rows=4, cols=3 ALGORITHM STEPS 1 Define State dp[row][col1][col2] = max cherries collected 2 Base Case Row 0: R1 at col 0, R2 at col 2 3 Transitions Each robot: 3 moves (down-left, down, down-right) 4 Compute Max Try all 9 combinations Handle overlap case DP Transition Formula for dc1 in [-1,0,1]: for dc2 in [-1,0,1]: cherries = grid[r][c1] if c1!=c2: +grid[r][c2] dp = max(dp, cherries+next) FINAL RESULT Optimal Robot Paths 3 1 1 2 5 1 1 5 5 2 1 1 Robot 1 3+2+1+2=8 Robot 2 1+1+5+5+1+1+1=16 + Output: 24 Maximum Cherries OK - Both robots reached bottom row optimally! Key Insight: Track both robots simultaneously! Use 3D DP where state = (row, robot1_col, robot2_col). At each row, try all 9 combinations of moves (3 choices per robot). When robots overlap, count cherries only once. Time: O(rows * cols^2 * 9) = O(rows * cols^2) TutorialsPoint - Cherry Pickup II | Optimal DP Solution
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