Chocolate Pickup (3D DP)
A hard Dynamic Programming problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
- Platform
- LeetCode
The problem
Given a grid of integers representing cherries, collect the maximum number of cherries by moving from top-left to bottom-right and back. Each cell can be collected only once.
Example 1
- Input
- grid = [[0,1,-1],[1,0,-1],[1,1,1]]
- Output
- 5
- Why
- The optimal path collects 5 cherries going down-right and back up-left.
Example 2
- Input
- grid = [[1,1,-1],[1,-1,1],[-1,1,1]]
- Output
- 0
- Why
- No path can collect cherries without hitting a -1 cell.
Example 3
- Input
- grid = [[1,1],[1,1]]
- Output
- 4
- Why
- Collect all cherries on both trips.
Constraints
- n == grid.length
- n == grid[i].length
- 1 <= n <= 50
- -1 <= grid[i][j] <= 1
How to think about it
Updated 2026-09-09A round trip from top-left to bottom-right and back is equivalent to two people walking forward from top-left to bottom-right simultaneously. If both walkers are on step t, their positions are (r1, t - r1) and (r2, t - r2). If they land on the same cell, cherries are collected only once.
Approaches, worst first
Sequential two-path greedy search
time O(n^2) · space O(n^2)
Find the best single path, clear its cherries, then find the best return path. Fails because the first path can greedily consume cherries that prevent a vastly superior combined dual path.
4D dynamic programming
time O(n^4) · space O(n^4)
dp[r1][c1][r2][c2] stores max cherries with person 1 at (r1, c1) and person 2 at (r2, c2). Transitions check four move combinations (down-down, down-right, right-down, right-right).
3D synchronized step DPWrite this one
time O(n^3) · space O(n^2)
Both people take step = r + c simultaneously. State is dp[step][r1][r2], where c1 = step - r1 and c2 = step - r2. Add grid[r1][c1] + (r1 != r2 ? grid[r2][c2] : 0) to max of 4 previous states.
Where people lose marks · 3
- Obstacle cells (-1): if either walker steps on -1, that joint state is completely unreachable and must be marked with -infinity.
- Double counting cherries: when r1 == r2 (and thus c1 == c2), add the cherry value only once.
- No valid path to destination: if (n-1, n-1) is unreachable, the function must return 0, not -infinity.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
What each operation costs
| Operation | Time |
|---|---|
| fill dynamic programming table of n states | O(n) |
| solve two-dimensional grid of m by n states | O(m * n) |
| space-optimized state transition keeping one row | O(n) |
What usually goes wrong with Dynamic Programming
- Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
- Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
- Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.
Which roles need this problem
Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Dynamic Programming problems
Problem set and role mapping as of .