AlgoMaster Logo

Cherry Pickup

hardFrequency8 min readUpdated June 23, 2026

Understanding the Problem

This reads like two separate path problems: go from top-left to bottom-right collecting cherries, then come back collecting more. The trips are not independent, though. Cherries picked on the first trip disappear, so the second trip sees a modified grid, and maximizing each trip on its own can lock in a suboptimal total.

Instead of one trip forward and one trip back, model the round trip as two people walking simultaneously from (0, 0) to (n-1, n-1). Person 1 represents the forward trip. Person 2 represents the return trip, reversed so it also runs top-left to bottom-right. Both move one step at a time, either right or down, and a cherry on a cell both occupy is counted once. This reformulation turns a sequential problem with interacting trips into a single DP over both walkers' positions.

Key Constraints:

  • 1 <= n <= 50 -> The grid is at most 50x50 = 2,500 cells. We have room for O(n^3) or even O(n^4) solutions.
  • grid[i][j] is -1, 0, or 1 -> Thorns block paths entirely, not just increase cost. We must handle unreachable states.

Approach 1: Greedy (Two Independent Trips)

Intuition

Solve the two trips independently: find the path from (0,0) to (n-1,n-1) that collects the most cherries, remove those cherries from the grid, then find a second maximum-cherry path on the modified grid (a return trip is equivalent to another forward trip with the moves reversed).

Each trip on its own is a standard maximum-path-sum DP, so this is easy to implement. It is also wrong. The best single path can take cherries positioned so that whatever remains cannot be covered by one more right/down path, while two coordinated paths, each individually suboptimal, collect more in total. The walkthrough below shows a grid where this happens.

Algorithm

  1. Use DP to find the path from (0,0) to (n-1,n-1) that collects the maximum cherries.
  2. Mark all cherries along that path as collected (set them to 0).
  3. Use DP again on the modified grid to find a second maximum-cherry path.
  4. Return the sum of cherries from both trips.

Example Walkthrough

1The grid holds 5 cherries. Trip 1 finds the single path that collects the most.
0
1
2
0
start
0
1
1
1
1
1
0
2
0
1
end
0
1/5

Code

On the walkthrough grid, the greedy split collects 4 cherries while the true maximum is 5. No fix to the first trip repairs this, because the two paths have to be chosen together. The next approach optimizes them as a single decision.

Approach 2: 3D DP (Two Simultaneous Walkers)

Intuition

Reversing the return trip makes the two-walker model exact: a path from (n-1,n-1) to (0,0) using left and up moves, read backwards, is a path from (0,0) to (n-1,n-1) using right and down moves, and it visits the same cells. The round trip is therefore two forward paths, chosen together.

Both walkers take exactly 2*(n-1) steps total to reach the destination. At each step, each walker moves either right or down. The number of steps taken so far, call it t, determines a constraint: if a walker is at row r, their column must be c = t - r. So with t fixed, we only need to track r1 (walker 1's row) and r2 (walker 2's row) to know both positions completely.

Cherry collection follows one rule: if both walkers are on the same cell, the cherry is counted once; otherwise each walker collects its own cell. This single rule is enough to prevent any cherry from being counted twice across the whole round trip.

Algorithm

  1. Let t range from 0 to 2*(n-1). This is the step number (how many moves each walker has made).
  2. For each step t, iterate over all valid (r1, r2) pairs where 0 <= r1, r2 <= min(t, n-1) and c1 = t - r1 and c2 = t - r2 are valid column indices.
  3. Both walkers arrived at their current position from the previous step. Each could have moved down (from row r-1) or right (from row r at previous step). This gives 4 predecessor combinations.
  4. Take the max over all 4 predecessor states. Add the cherries at both current positions. If both walkers are on the same cell, add the cherry value only once.
  5. After the final step t = 2*(n-1), both walkers are at (n-1, n-1), so the answer is dp[n-1][n-1]. If it is still negative, no valid path pair exists; return 0. (Each step only reads the previous step's values, so the implementation keeps two 2D arrays instead of a full 3D table.)

Example Walkthrough

1Step t=0: Both walkers start at (0,0). Cherry=0. dp[0][0]=0
0
1
2
0
W1+W2
0
1
thorn
-1
1
1
0
thorn
-1
2
1
1
1
1/5

Code

The same recurrence can also be written top down. Recursion with memoization removes the manual loop bounds and buffer swapping, at the cost of an O(n^3) memo table instead of two O(n^2) arrays.

Approach 3: Top-Down Memoization

Intuition

The top-down version asks, from a given pair of positions, what is the best both walkers can still collect on the way to (n-1, n-1). Define a function solve(r1, c1, r2) that returns that maximum. Both walkers have taken the same number of steps, so c2 = r1 + c1 - r2 and does not need to be passed. Each call tries the 4 movement combinations (each walker goes right or down) and takes the maximum over the valid ones.

Algorithm

  1. Define solve(r1, c1, r2) where c2 = r1 + c1 - r2.
  2. If any position is out of bounds or a thorn, return negative infinity.
  3. Base case: if r1 == n-1 and c1 == n-1, walker 1 is at the destination, and so is walker 2: the bounds check already passed, and c2 = 2*(n-1) - r2 <= n-1 forces r2 = n-1. Return grid[n-1][n-1], counted once.
  4. Compute cherries at current positions (once if same cell, both if different).
  5. Try all 4 movement combinations: (down,down), (down,right), (right,down), (right,right).
  6. Return current cherries + max of all 4 recursive results.
  7. Memoize using a 3D table indexed by (r1, c1, r2).

Example Walkthrough

1solve(0,0,0): Both at (0,0). Cherry=0. Try 4 moves.
0
1
2
0
W1+W2
0
1
-1
1
1
0
-1
2
1
1
1
1/5

Code