Minimum range boosts to make server power non-decreasing
You manage a row of n machines. Machine i has an integer capacity power[i]. You want the capacities to be non-decreasing from left to right (power[i] <= power[i+1] for every i).
You may apply the following operation any number of times:
- Pick a contiguous range
[l, r]and a positive integerx, then addxto every machine in that range.
The cost of a plan is the sum of the x values across all operations. The number of operations does not matter. Return the minimum possible cost.
Implement:
def min_increase_sum(power: list[int]) -> int: ...
Constraints
0 <= n <= 10^51 <= power[i] <= 10^9- The answer may exceed 32 bits.
Example 1
power = [3, 1, 2]
output: 2
Add 2 to the range [1, 2], giving [3, 3, 4]. Raising each element to the running maximum would cost 3. A single range operation does better.
Example 2
power = [5, 2, 4, 1, 6]
output: 6
Add 3 to [1, 4] to get [5, 5, 7, 4, 9]. Then add 3 to [3, 4] to get [5, 5, 7, 7, 12].
Be ready to explain why no cheaper plan exists, and to return the operations themselves.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
