Cherry Pickup II
Pattern: 2D grid DP
Two robots start at the top-left and top-right and move down one row at a time, each to the cell below, below-left or below-right. The state is (row, column of robot 1, column of robot 2); a shared cell's cherries count once.
The problem
Given a grid of cherries, robot 1 starts at (0, 0) and robot 2 at (0, cols - 1). Each step both move down one row, to the same column or one to either side. Cherries are picked when a robot passes; if both are on one cell, it is counted once. Return the most cherries.
Think first
- Why can the robots not be planned one at a time?
- What describes both robots after r rows?
- How many moves per step?
Hints
Brute force, and why it fails
Try every pair of paths: 9^rows.
The key insight
Moving both robots together keeps the state small: two columns per row.
Dry run
| r1 | 12 |
|---|
grid [[3, 1, 1], [2, 5, 1], [1, 5, 5], [2, 1, 1]]. Robot 1 takes 3, 2, 5, 2.
Step 1 of 3
The plan
best[a][b] for the row below; for each row from the bottom: here + max over 9 moves of best[a'][b']
Complexity
- Time
O(R × C² × 9)- Space
O(C²)
One C × C table per row.
Edge cases
- One row.
- Two columns.
- Both robots on one cell.
I've built my logic, show the code
Java
class Solution {
public int cherryPickup(int[][] grid) {
int R = grid.length, C = grid[0].length;
int[][] best = new int[C][C]; // best[a][b]: the most cherries from this row down, robots at columns a and b
for (int r = R - 1; r >= 0; r--) {
int[][] next = new int[C][C];
for (int a = 0; a < C; a++)
for (int b = 0; b < C; b++) {
int here = grid[r][a] + (a == b ? 0 : grid[r][b]); // a shared cell is picked once
int below = 0;
if (r < R - 1) {
below = Integer.MIN_VALUE;
for (int da = -1; da <= 1; da++) // each robot moves down-left, down or down-right
for (int db = -1; db <= 1; db++) {
int na = a + da, nb = b + db;
if (na >= 0 && nb >= 0 && na < C && nb < C) below = Math.max(below, best[na][nb]);
}
}
next[a][b] = here + below;
}
best = next;
}
return best[0][C - 1];
}
}C++
class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
int R = static_cast<int>(grid.size()), C = static_cast<int>(grid[0].size());
vector<vector<int>> best(C, vector<int>(C, 0)); // the most cherries from this row down, robots at a and b
for (int r = R - 1; r >= 0; r--) {
vector<vector<int>> next(C, vector<int>(C, 0));
for (int a = 0; a < C; a++)
for (int b = 0; b < C; b++) {
int here = grid[r][a] + (a == b ? 0 : grid[r][b]); // a shared cell is picked once
int below = 0;
if (r < R - 1) {
below = INT_MIN;
for (int da = -1; da <= 1; da++) // each robot moves down-left, down or down-right
for (int db = -1; db <= 1; db++) {
int na = a + da, nb = b + db;
if (na >= 0 && nb >= 0 && na < C && nb < C) below = max(below, best[na][nb]);
}
}
next[a][b] = here + below;
}
best = next;
}
return best[0][C - 1];
}
};Python
class Solution:
def cherryPickup(self, grid: list[list[int]]) -> int:
R, C = len(grid), len(grid[0])
best = [[0] * C for _ in range(C)] # the most cherries from this row down, robots at columns a and b
for r in range(R - 1, -1, -1):
nxt = [[0] * C for _ in range(C)]
for a in range(C):
for b in range(C):
here = grid[r][a] + (0 if a == b else grid[r][b]) # a shared cell is picked once
below = 0 if r == R - 1 else max(best[na][nb] for na in (a - 1, a, a + 1) for nb in (b - 1, b, b + 1)
if 0 <= na < C and 0 <= nb < C) # each robot moves down by one
nxt[a][b] = here + below
best = nxt
return best[0][C - 1]Common mistakes
- Counting a shared cell twice.
- Letting a robot leave the grid.