Best Haul from a Row of Cash Stations
Dynamic ProgrammingAuto-graded · 22 test casesLast updated
Tests one-dimensional dynamic programming: pick values from a sequence so no two neighbors are both chosen, maximizing the total.
Cash Stations in a Row
A line of cash stations is laid out one after another. The list amounts gives the cash stored at each station, in order.
Because of a safety rule, if you collect from a station you may not collect from the station directly before it or directly after it. In other words, no two collected stations may be neighbors in the list. You are free to skip as many stations as you like.
Compute the largest total amount of cash you can collect while obeying this rule.
Input
amounts: a list of integers, whereamounts[i]is the cash at stationi.
Output
- A single integer: the maximum total that can be collected with no two neighboring stations both chosen.
Notes
- Stations are neighbors only if their indices differ by exactly 1.
- Choosing no stations at all is allowed and yields a total of
0. This matters only if every amount is non-positive, which falls outside the stated constraints.
Constraints
1 <= len(amounts) <= 10^50 <= amounts[i] <= 10^9
Examples
Example 1
Input: max_cash_collected([1, 2, 3, 1])
Output: 4
Example 2
Input: max_cash_collected([2, 7, 9, 3, 1])
Output: 12
Hints
Hint 1
Look at the last station only. Either you collect from it or you don't. What does each choice tell you about the stations before it?
Hint 2
If you collect from station i, you must ignore station i-1, so the best total is amounts[i] plus the best result over stations 0..i-2. If you skip it, the best total is the best result over stations 0..i-1.
Hint 3
Each step needs only the best results for the previous two prefixes, so two rolling variables are enough instead of a whole table. Start both at 0 so short lists work with no special case.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Scan the stations from left to right and track the best total achievable using only the stations seen so far. Let best[i] be the best total using stations 0..i. For station i there are two options:
- Skip it. The best total is
best[i-1]. - Collect it. Station
i-1is then off limits, so the best total isbest[i-2] + amounts[i].
So best[i] = max(best[i-1], best[i-2] + amounts[i]), with both best[-1] and best[-2] equal to 0 (no stations, no cash).
Since best[i] depends only on the previous two values, we keep two variables, two_back and one_back, and update them in one step per station. The answer is one_back after the last station.
Why it is correct
Consider an optimal selection over stations 0..i.
- If it does not include station
i, it is a valid selection over0..i-1, so its value is at mostbest[i-1], andbest[i-1]is achievable. - If it includes station
i, it cannot include stationi-1. Removing stationileaves a valid selection over0..i-2, worth at mostbest[i-2]. Adding stationito the optimal selection over0..i-2is valid, sobest[i-2] + amounts[i]is achievable.
Every valid selection falls into one of these two cases, so the maximum of the two values is the true optimum. Starting both rolling values at 0 makes lists of length 1 or 2 work without special handling.
Complexity
- Time: O(n), one pass with constant work per element.
- Space: O(1), two variables.
Reference solution
from typing import List
def max_cash_collected(amounts: List[int]) -> int:
two_back = 0
one_back = 0
for value in amounts:
two_back, one_back = one_back, max(one_back, two_back + value)
return one_back