Maximum Score With No Two Adjacent Drops
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Tests whether a candidate can model a pick/skip DP with a "no two consecutive skips" constraint using O(1) space and O(n) time.
Problem
You are given a list of integers representing the point values of stations along a route. Values may be positive, negative, or zero.
Your goal is to collect points from a subset of stations to produce the highest possible total. There is one hard rule: you may never bypass two stations in a row. For every pair of neighboring stations at positions i and i + 1, your chosen subset must include at least one of them.
You must stop at at least one station.
Return the maximum total points you can collect from any valid subset.
Constraints
1 <= len(scores) <= 1_000_000-10^9 <= scores[i] <= 10^9- Your solution must run in O(n) time with O(1) auxiliary space.
Examples
Example 1
Input: max_route_score([9, -1, -3, 4, 5])
Output: 17
Example 2
Input: max_route_score([-5, -2, -3])
Output: -2
Example 3
Input: max_route_score([-10])
Output: -10
Hints
Hint 1
Think about what information you actually need from the previous step to make the current decision. You don't need the full history — only whether you stopped at the last station or skipped it.
Hint 2
Track three running values: the best total when you stopped at the current station, the best total when you skipped it (but only if the previous station was a stop), and a sentinel for 'nothing collected yet' that covers skipping the very first station.
Hint 3
After processing all stations, the answer is the best value among your 'stopped' and 'skipped' states — never the empty sentinel, since you must collect at least one score. Watch out for the n=1 edge case where the skipped state never becomes valid.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core observation
At any position the only thing that constrains your next decision is what you did at the current position: did you stop, or did you skip? You never need to remember anything further back. That means the entire DP state fits in a fixed number of variables.
States
After processing position i, track three values:
| Variable | What it represents |
|---|---|
stopped | Best total over all valid subsets of scores[0..i] where station i is included. |
skipped | Best total where station i is excluded and station i-1 was included (so no double-skip). |
empty | Station i was skipped and nothing has been collected yet. Only possible after skipping station 0. |
empty is kept separate from skipped because it carries a contribution of 0 before any real score is added. Without it, the transition "skip station 0, then take station 1" would be missed. It also prevents the empty selection from being returned when n == 1.
Initialization (station 0)
stopped = scores[0] # took the only station seen so far
skipped = None # cannot have skipped station 0 with a non-empty selection
empty = 0 # skipped station 0, nothing chosen yet
Transition (stations 1 through n-1)
best_prev = max of all non-None states from the previous step
next_stopped = best_prev + scores[i] # always legal
next_skipped = stopped # skip only when previous was a stop
empty = None # "nothing chosen" is illegal from index 1 onward
Answer
max(stopped, skipped), ignoring None. When n == 1, skipped is still None, so we return scores[0] directly — even if it is negative, because at least one station is required.
Why it is correct
Every valid subset corresponds to a sequence of stop/skip decisions with no two adjacent skips. Each prefix of that sequence ends in exactly one of the three states. The transitions enumerate every legal move from each state and none of the illegal ones (you can only skip when the previous was a stop). Each variable keeps the maximum over all paths that end in its state, and future transitions depend only on the current state — not on how you arrived there. This is the standard optimal-substructure argument for 1-D DP.
An equivalent framing: the answer equals sum(scores) − (sum of skipped values). The skipped indices form an independent set (no two adjacent), and they cannot cover every index. Minimising the skipped sum is precisely the classic "house robber" problem.
Worked trace — [9, -1, -3, 4, 5]
| i | value | stopped | skipped | empty |
|---|---|---|---|---|
| 0 | 9 | 9 | – | 0 |
| 1 | −1 | max(9, 0) − 1 = 8 | 9 | – |
| 2 | −3 | max(8, 9) − 3 = 6 | 8 | – |
| 3 | 4 | max(6, 8) + 4 = 12 | 6 | – |
| 4 | 5 | max(12, 6) + 5 = 17 | 12 | – |
Answer: max(17, 12) = 17.
Complexity
- Time: O(n) — one pass, constant work per element.
- Space: O(1) auxiliary — three scalar variables regardless of input size. The loop uses index-based iteration to avoid creating a slice of the list.
Edge cases
- n = 1:
skippedisNone; the answer isscores[0], even when negative. - All negative: the DP skips as many as the constraint allows, returning the least-negative single element (or pair) it cannot avoid.
- n = 2, both negative:
emptylets the solution take only station 1, andskippedcovers taking only station 0; the larger of the two is returned. - All non-negative: every station is taken; the sum of the full list is returned.
- Large magnitudes: sums can reach roughly 10¹⁵; Python's arbitrary-precision integers handle this without overflow.
Reference solution
from typing import List
def max_route_score(scores: List[int]) -> int:
# stopped: best total when the current station is included
# skipped: best total when the current station is excluded
# (only valid if the previous station was included)
# empty: no station collected yet; only alive while we could
# still skip the very first station without violating rules
stopped = scores[0]
skipped = None
empty = 0 # represents "skipped station 0, nothing chosen yet"
for i in range(1, len(scores)):
value = scores[i]
# Best total we can carry into this station from any legal prior state
best_prev = stopped
if skipped is not None:
best_prev = max(best_prev, skipped)
if empty is not None:
best_prev = max(best_prev, empty)
# Taking this station is always legal regardless of prior state
next_stopped = best_prev + value
# Skipping this station is only legal if the previous was taken
next_skipped = stopped
stopped, skipped, empty = next_stopped, next_skipped, None
return stopped if skipped is None else max(stopped, skipped)