This question evaluates algorithmic problem-solving skills in array manipulation and optimization, focusing on reasoning about range-increment operations, cost minimization, and preserving non-decreasing order.
You are given an integer array power[0..n-1] representing computational power of n servers.
You may perform the following operation any number of times:
[l, r]
(0-indexed,
0 <= l <= r < n
)
x >= 0
x
(i.e., for all
i in [l, r]
, set
power[i] += x
)
Your goal is to make the final array non-decreasing (i.e., power[i] <= power[i+1] for all valid i).
Let the “cost” be the sum of all chosen x values across all operations (segment length does not affect cost). Compute the minimum possible cost.
Example:
n = 5
,
power = [3, 4, 1, 6, 2]
3
to subarray
[2, 4]
→
[3, 4, 4, 9, 5]
4
to subarray
[4, 4]
→
[3, 4, 4, 9, 9]
3 + 4 = 7
Complete the function:
findMinimumSum(power)
→ returns an integer: the minimum achievable total cost.
Assumptions:
power[i]
are integers; only increases are allowed (
x >= 0
).