Best Haul from a Row of Stalls Without Adjacent Picks
Dynamic ProgrammingAuto-graded · 24 test casesLast updated
Pick values from a row so that no two chosen positions are neighbors, maximizing the total, which tests recognizing and implementing a simple one-dimensional dynamic programming recurrence.
Stall Row Collection
A market has a row of stalls, numbered from left to right. Stall i offers a prize worth prizes[i] points. You walk along the row and may collect the prize from any stall you like, but a guard watches every stall you collect from, so you are not allowed to collect from two stalls that sit next to each other (indices i and i + 1).
You may skip as many stalls as you want, including all of them.
Write a function that returns the largest total number of points you can collect while obeying this rule.
Details
- Stalls that are not adjacent (for example indices
0and2) may both be chosen. - If there are no stalls, the answer is
0. - Every prize is non-negative, so collecting nothing is always allowed and contributes
0. - The function returns a single integer, the maximum achievable total.
Constraints
0 <= len(prizes) <= 10^50 <= prizes[i] <= 10^4
Examples
Example 1
Input: max_non_adjacent_haul([1, 2, 3, 1])
Output: 4
Example 2
Input: max_non_adjacent_haul([2, 7, 9, 3, 1])
Output: 12
Example 3
Input: max_non_adjacent_haul([])
Output: 0
Example 4
Input: max_non_adjacent_haul([5])
Output: 5
Hints
Hint 1
Look at just the last stall. Either you collect from it or you do not. What does each choice imply about the stall before it?
Hint 2
Let best(i) be the top total using only the first i stalls. Then best(i) = max(best(i-1), best(i-2) + prizes[i-1]).
Hint 3
The recurrence only reads the previous two results, so two variables are enough instead of a full table.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Define best(i) as the maximum total using only the first i stalls. For the i-th stall (value p) there are two options:
- Skip it: the total is
best(i-1). - Collect it: the neighboring stall before it must be skipped, so the total is
best(i-2) + p.
Therefore best(i) = max(best(i-1), best(i-2) + p), with best(0) = 0 and best(-1) = 0.
Because each step only needs the previous two values, we keep them in two variables, two_back and one_back, and update both at once as we scan the list. The answer is one_back after the last stall.
Why it is correct
Any valid selection on the first i stalls either includes stall i or it does not. If it does, stall i-1 is excluded and the rest is a valid selection on the first i-2 stalls, so the best such total is best(i-2) + p. If it does not, the rest is a valid selection on the first i-1 stalls, giving best(i-1). Taking the maximum of both covers every possibility, so by induction the recurrence is optimal. Greedy ideas such as always picking the biggest prize fail, for instance on [2, 1, 1, 2], where the best is 4 from indices 0 and 3.
Edge cases: an empty list never enters the loop and returns 0; a single stall returns its value; all-zero input returns 0.
Complexity
- Time: O(n)
- Space: O(1)
Reference solution
from typing import List
def max_non_adjacent_haul(prizes: List[int]) -> int:
two_back = 0 # best total considering stalls up to two positions earlier
one_back = 0 # best total considering stalls up to the previous position
for prize in prizes:
two_back, one_back = one_back, max(one_back, two_back + prize)
return one_back