Best Haul from a Line of Stalls Without Neighbors
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Compute the maximum total from a line of non-negative numbers when no two neighboring entries may both be picked, using linear-time dynamic programming.
A market street has a straight line of stalls, and stall i holds a non-negative integer amount of coins, coins[i]. You may collect from any set of stalls, but you may never collect from two stalls that sit directly next to each other (indices i and i + 1).
Return the largest total number of coins you can collect.
Details
- The street is a straight line, not a loop. The first and last stalls are not neighbors.
- Collecting from no stalls at all is allowed, so the result is never negative.
- Return only the maximum total as a single integer. It does not matter which stalls reach that total if several do.
- If the line is empty, return
0.
Function
Implement max_haul(coins), which takes a list of integers and returns an integer.
Constraints
0 <= len(coins) <= 1000000 <= coins[i] <= 10000- The answer is at most 500,000,000 (at most 50,000 stalls can be chosen).
Examples
Example 1
Input: max_haul([2, 7, 9, 3, 1])
Output: 12
Example 2
Input: max_haul([4, 1, 1, 4])
Output: 8
Example 3
Input: max_haul([])
Output: 0
Hints
Hint 1
When you reach a stall, the only fact about the past that restricts you is whether you collected from the previous stall.
Hint 2
Track two numbers for the prefix seen so far: the best total if the latest stall was collected, and the best total if it was not.
Hint 3
Collecting from the current stall requires the previous one to be skipped; skipping the current one allows either previous state. Update both numbers at the same time from the old values.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Scan the stalls from left to right and keep two values for the prefix processed so far:
took: the best total when the most recent stall is collected.skipped: the best total when the most recent stall is not collected.
Both start at 0, representing the empty prefix. For each new amount c:
- Collecting it requires that the previous stall was not collected, so the new
tookisskipped + c. - Not collecting it leaves the previous stall free in either state, so the new
skippedismax(took, skipped).
Both updates must use the old values, so they are done in a single tuple assignment. The answer is max(took, skipped) after the last stall.
Why it is correct
After processing stall i, took is the best valid total over stalls 0..i that includes stall i, and skipped is the best that excludes it. A selection that includes stall i cannot include stall i-1, so its best value is the best "excluding i-1" total plus coins[i]. A selection that excludes stall i has no restriction on stall i-1, so its best value is the larger of the two previous states. Every valid selection either includes or excludes the last stall, so the final maximum covers all cases.
Edge cases follow naturally: an empty list gives max(0, 0) = 0, a single stall gives its own value, two stalls give the larger of the two, and since the first and last stalls are independent there is no wrap-around logic.
Complexity
- Time: O(n), one constant-time update per stall.
- Space: O(1), only two integers are stored. No recursion is used, so long inputs are safe.
Reference solution
from typing import List
def max_haul(coins: List[int]) -> int:
# took: best total so far where the latest stall is collected
# skipped: best total so far where the latest stall is not collected
took, skipped = 0, 0
for c in coins:
took, skipped = skipped + c, max(took, skipped)
return max(took, skipped)