KatInterview

Maximum Score With No Two Adjacent Drops

Dynamic ProgrammingAuto-graded · 23 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingarraysgreedy

Last 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.