Sentinels on a Road: Best Covered Value With Single Left Steps
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Maximize the total value of covered towns on a line when each sentinel may step one town left at most once into an empty town, by finding which town each run of sentinels leaves uncovered.
Sentinels on a Road
A road has n towns in a row, numbered from left to right starting at 1. You are given:
values: a list ofnnon-negative integers, wherevalues[i]is the value of towni + 1.guards: a string of lengthnmade of'0'and'1'. Ifguards[i] == '1', a sentinel starts in towni + 1. If it is'0', that town starts empty.
Each town holds at most one sentinel at any moment.
Moves
Each sentinel may take at most one step in total. A step moves a sentinel from its town to the town immediately to its left. The step is allowed only if:
- the sentinel is not in the leftmost town, and
- the town to its left holds no sentinel at that moment.
Steps happen one at a time, and you choose the order. A town that was vacated earlier can therefore receive a sentinel later. A sentinel that has already stepped cannot step again.
Goal
After you finish moving, the covered value is the sum of values over all towns that contain a sentinel. Return the largest covered value you can reach. You may also choose to make no moves at all.
Constraints
1 <= n <= 10^5len(values) == len(guards) == n0 <= values[i] <= 10^9guardscontains only the characters'0'and'1'- The answer may exceed 32-bit range.
Examples
Example 1
Input: max_covered_value([10, 5, 8, 9, 6], '01101')
Output: 27
Hints
Hint 1
Sentinels only move left, and only by one town. Ask which sentinels can ever influence each other.
Hint 2
Look at a maximal block of consecutive '1's with an empty town directly to its left. If the block has k sentinels, they end up in k of the k+1 towns formed by the block plus that empty town. Which towns can be the one left uncovered?
Hint 3
Any town in that group can be the uncovered one if you shift sentinels left-to-right up to it. So each block with an empty left neighbor gains the left neighbor's value minus the cheapest town in the block, if that is positive. Blocks at the very start cannot move.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sentinels only move left, and by at most one town. This splits the road into independent pieces.
Take a maximal block of consecutive '1' towns, say k sentinels in towns s .. s+k-1.
- If
sis the first town, there is no town to the left. None of these sentinels can move, so they keep their value. - Otherwise town
s-1is'0', so it starts empty. Each sentinel in the block ends in its own town or the one to its left, so allkof them end up in thek+1townss-1 .. s+k-1. Exactly one of those towns is left uncovered.
We can choose which town e stays uncovered. Move the sentinels at s, s+1, ..., e one step left, in left-to-right order. Each one moves into a town that was just vacated, or into the original empty town s-1. Sentinels to the right of e stay put.
Choosing e as the town with the smallest value in the block gives the best result. Leaving the empty town s-1 itself uncovered is the same as making no moves, which is also allowed. So the block contributes:
- its original sum, plus
max(0, values[s-1] - min(values[s .. s+k-1])).
The first term is the baseline: the sum over all '1' towns. The second term is the gain, which is never negative because we may choose not to move.
Why it is correct
- Independence: A sentinel from a block can only reach towns inside that block or the single
'0'town just before it. Different blocks are separated by at least one'0', and that'0'is the one adjacent to only the block on its right. Sentinels to its left cannot move right, so the blocks never compete for the same empty town. - Upper bound: Final positions are distinct, so
ksentinels fill exactlykof thek+1available towns. The best total for the block is therefore the sum of thek+1towns minus the smallest of them, which equals the baseline plusmax(0, values[s-1] - min(block)). - Achievability: The left-to-right shifting order above realizes any chosen uncovered town, and each sentinel moves at most once.
Complexity
- Time: O(n), since each index is visited once and each block minimum is computed over that block only.
- Space: O(1) extra (the slice used for
mincan be replaced by a running minimum).
Reference solution
from typing import List
def max_covered_value(values: List[int], guards: str) -> int:
total = sum(v for v, g in zip(values, guards) if g == '1')
n = len(guards)
i = 0
while i < n:
if guards[i] == '0':
i += 1
continue
start = i
while i < n and guards[i] == '1':
i += 1
# The block can shift left only if an empty town sits right before it.
if start > 0:
gain = values[start - 1] - min(values[start:i])
total += max(0, gain)
return total