Minimum Operations to Achieve At Least K Peaks Problem
Description
LeetCode Problem 3892.
You are given a circular integer array nums of length n.
An index i is a peak if its value is strictly greater than its neighbors:
- The previous neighbor of
iisnums[i - 1]ifi > 0, otherwisenums[n - 1]. - The next neighbor of
iisnums[i + 1]ifi < n - 1, otherwisenums[0].
You are allowed to perform the following operation any number of times:
- Choose any index
iand increasenums[i]by 1.
Return an integer denoting the minimum number of operations required to make the array contain at least k peaks. If it is impossible, return -1.
Example 1:
Input: nums = [2,1,2], k = 1
Output: 1
Explanation:
- To achieve at least k = 1 peak, we can increase nums[2] = 2 to 3.
- After this operation, nums[2] = 3 is strictly greater than its neighbors nums[0] = 2 and nums[1] = 1.
- Therefore, the minimum number of operations required is 1.
Example 2:
Input: nums = [4,5,3,6], k = 2
Output: 0
Explanation:
- The array already contains at least k = 2 peaks with zero operations.
- Index 1: nums[1] = 5 is strictly greater than its neighbors nums[0] = 4 and nums[2] = 3.
- Index 3: nums[3] = 6 is strictly greater than its neighbors nums[2] = 3 and nums[0] = 4.
- Therefore, the minimum number of operations required is 0.
Example 3:
Input: nums = [3,7,3], k = 2
Output: -1
Explanation:
It is impossible to have at least k = 2 peaks in this array. Therefore, the answer is -1.
Constraints:
2 <= n == nums.length <= 5000-10^5 <= nums[i] <= 10^50 <= k <= n
Solution
Approach: Dynamic Programming on a Circle
To make index i a peak it is enough to raise nums[i] until it beats both of its (circular) neighbours, which costs cost[i] = max(0, max(prev, next) + 1 - nums[i]). Two neighbouring indices can never both be peaks, so we need to choose k pairwise non-adjacent indices on a circle with the smallest total cost. Raising a chosen index never hurts another chosen one, because chosen indices are never adjacent, and indices that are already peaks cost 0. At most n / 2 peaks fit on a circle, so the answer is -1 when 2k > n, and 0 when k = 0.
Choosing k non-adjacent items on a line is a standard DP: f[j] is the minimum cost of having chosen j items where the last item is not chosen, and g[j] the same where the last item is chosen. Going through the items, f[j] becomes min(f[j], g[j]) and g[j] becomes f[j - 1] + cost.
To handle the circle, split on index 0. Either it is not a peak, and we solve the line 1..n-1 for k peaks. Or it is a peak, and indices 1 and n - 1 are excluded, so we add cost[0] to the line 2..n-2 for k - 1 peaks. The answer is the smaller of the two.
Complexity:
- Time complexity:
O(n * k). - Space complexity:
O(k).
INF = 10**15
def pick(c: List[int], k: int) -> int:
# minimum total cost of choosing exactly k pairwise non-adjacent entries of c
f = [INF] * (k + 1) # the last entry is not chosen
g = [INF] * (k + 1) # the last entry is chosen
f[0] = 0
for x in c:
nf = [min(a, b) for a, b in zip(f, g)]
ng = [INF] + [a + x for a in f[:k]]
f, g = nf, ng
return min(f[k], g[k])
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
n = len(nums)
if k == 0:
return 0
if 2 * k > n:
return -1
# cost[i]: operations needed to make index i a peak
cost = [max(0, max(nums[i - 1], nums[(i + 1) % n]) + 1 - nums[i]) for i in range(n)]
# index 0 is not a peak, or it is (then its two neighbours are not)
ans = min(pick(cost[1:], k), cost[0] + pick(cost[2:n - 1], k - 1))
return ans if ans < INF else -1