Cherry Pickup II Problem
Description
LeetCode Problem 1463.
You are given a rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell.
You have two robots that can collect cherries for you:
- Robot #1 is located at the top-left corner
(0, 0), and - Robot #2 is located at the top-right corner
(0, cols - 1).
Return the maximum number of cherries collection using both robots by following the rules below:
- From a cell
(i, j), robots can move to cell(i + 1, j - 1),(i + 1, j), or(i + 1, j + 1). - When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
- When both robots stay in the same cell, only one takes the cherries.
- Both robots cannot move outside of the grid at any moment.
- Both robots should reach the bottom row in
grid.
Example 1:

Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.
Example 2:

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
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.
Constraints:
rows == grid.lengthcols == grid[i].length2 <= rows, cols <= 700 <= grid[i][j] <= 100
Solution
Approach: Dynamic Programming over Both Robots' Columns
Both robots move down exactly one row per step, so after i steps they are both on row i. A state therefore only needs the two columns: dp[a][b] is the most cherries collected so far with robot 1 in column a and robot 2 in column b on the current row.
On row 0 the only reachable state is dp[0][cols - 1] = grid[0][0] + grid[0][cols - 1]; every other pair is marked unreachable (-1). To move to the next row, each robot came from one of three columns above it (a - 1, a, a + 1, and the same for b), so
new[a][b] = max over the 9 earlier pairs of dp[pa][pb] + cherries(a, b),
where cherries(a, b) is grid[i][a] + grid[i][b], or just grid[i][a] when a == b, because a cell that both robots visit is only picked once. Pairs whose 9 predecessors are all unreachable stay unreachable. The answer is the largest value on the last row.
Only the previous row is needed, so two cols x cols tables are enough.
Complexity: O(rows · cols2) time (with a factor of 9 for the predecessors) and O(cols2) extra space.
class Solution:
def cherryPickup(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
# prev[a][b]: most cherries with robot 1 in column a and robot 2 in column b
# on the previous row; -1 means that pair of columns cannot be reached.
prev = [[-1] * n for _ in range(n)]
prev[0][n - 1] = grid[0][0] + grid[0][n - 1]
for i in range(1, m):
row = grid[i]
cur = [[-1] * n for _ in range(n)]
for a in range(n):
for b in range(n):
best = -1
for pa in range(max(0, a - 1), min(n, a + 2)):
prow = prev[pa]
for pb in range(max(0, b - 1), min(n, b + 2)):
if prow[pb] > best:
best = prow[pb]
if best >= 0:
# a shared cell is picked only once
cur[a][b] = best + row[a] + (row[b] if a != b else 0)
prev = cur
return max(max(r) for r in prev)