Fewest Segment Boosts to Make a Sequence Non-Decreasing
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Compute the minimum number of +1 contiguous-segment updates needed to make an array non-decreasing, using adjacent differences and a lower-bound argument.
Fewest Segment Boosts
You are given a list values of n integers. In one boost, you pick any contiguous segment of the list (from position l to position r, with 0 <= l <= r < n) and add exactly 1 to every element in that segment. Each boost costs 1, no matter how long the segment is.
You may perform as many boosts as you like, and segments may overlap or repeat. Your goal is to make the list non-decreasing, meaning values[i] <= values[i + 1] for every valid i.
Return the minimum number of boosts needed. If the list is already non-decreasing (including when it has a single element), return 0.
Only boosts (adding 1) are allowed; elements can never be decreased.
Function
Implement min_segment_boosts(values), which takes the list of integers and returns a single integer: the minimum number of boosts.
Constraints
1 <= n <= 10^50 <= values[i] <= 10^9- The answer can be as large as about
10^14, so make sure your arithmetic does not overflow in languages with fixed-width integers.
Examples
Example 1
Input: min_segment_boosts([3, 2, 1])
Output: 2
Example 2
Input: min_segment_boosts([1, 2, 3])
Output: 0
Hints
Hint 1
Think about the differences between neighbours, d[i] = values[i] - values[i-1]. The list is non-decreasing exactly when all of these are at least 0.
Hint 2
A boost on [l, r] changes only two differences: d[l] goes up by 1 and d[r+1] goes down by 1 (if r+1 exists). Which differences can a boost actually help?
Hint 3
Each negative difference at position i can only be raised by boosts that start at i, so you need at least its deficit in boosts there. Show that boosting the suffix starting at i achieves this without hurting any other difference.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Work with the adjacent differences d[i] = values[i] - values[i-1] for i >= 1. The list is non-decreasing exactly when every d[i] >= 0.
A boost on segment [l, r] affects only two differences:
d[l]increases by 1, sincevalues[l]rises butvalues[l-1]does not (ifl = 0there is no such difference).d[r+1]decreases by 1, ifr + 1 < n.
Differences strictly inside the segment are unchanged because both neighbours rise together.
So the answer is the sum of all the drops:
answer = sum(max(0, values[i-1] - values[i]) for i in 1..n-1)
Why it is correct
Lower bound. The only way to increase d[i] is a boost that starts exactly at i, and each such boost raises it by just 1. A drop of size D at position i therefore needs at least D boosts starting at i. Every boost has one start position, so boosts counted for different positions are distinct, and the total is at least the sum of all drops.
Achievability. For each drop of size D at position i, apply D boosts to the suffix [i, n-1]. A suffix boost has no right end inside the list, so it raises d[i] and lowers no other difference. After all of these, every difference is non-negative, and exactly the lower-bound number of boosts has been used.
The two bounds match, so the sum is optimal.
Common wrong ideas such as max - min or "the largest single drop" fail because drops at different positions need separate start points. For example, [5, 0, 5, 0] needs 10 boosts.
Complexity
- Time: O(n), a single pass over adjacent pairs.
- Space: O(1) extra.
Edge cases
n = 1, all-equal, or already sorted lists give0.- A strictly decreasing list gives
values[0] - values[-1], since the drops telescope. - Totals up to about
10^14are fine for Python integers.
Reference solution
from typing import List
def min_segment_boosts(values: List[int]) -> int:
# Each drop between neighbours must be repaired by boosts that start
# at the right-hand element; summing all drops gives a lower bound,
# and suffix boosts achieve it.
total = 0
for i in range(1, len(values)):
if values[i] < values[i - 1]:
total += values[i - 1] - values[i]
return total