Minimum Operations to Make Mountain Array I Problem
Description
LeetCode Problem 4118.
You are given an integer array nums of length n.
In one operation, you may increment or decrement any element of nums by 1.
All elements of nums must remain positive (at least 1) after every operation.
An array is called a mountain array if there exists an index peak such that 0 < peak < n - 1 and:
nums[0] < nums[1] < ... < nums[peak]nums[peak] > nums[peak + 1] > ... > nums[n - 1]
Return the minimum number of operations required to transform nums into a mountain array. If it is impossible, return -1.
Example 1:
Input: nums = [2,2,3]
Output: 2
Explanation:
One optimal way is to:
- Increment nums[1] by 1 and decrement nums[2] by 1.
- The resulting array is [2, 3, 2], which is a mountain array with peak = 1.
- Therefore, the total number of operations is 1 + 1 = 2.
Example 2:
Input: nums = [9,4,9]
Output: 6
Explanation:
One optimal way is to:
- Increment nums[1] by 1 six times.
- The resulting array is [9, 10, 9], which is a mountain array with peak = 1.
- Therefore, the total number of operations is 6.
Example 3:
Input: nums = [5,7,7,4]
Output: 1
Explanation:
One optimal way is to:
- Decrement nums[1] by 1.
- The resulting array is [5, 6, 7, 4], which is a mountain array with peak = 2.
- Therefore, the total number of operations is 1.
Example 4:
Input: nums = [1,2]
Output: -1
Explanation:
A mountain array must have a peak with at least one element on each side. Since n = 2 and operations do not change the array's length, forming a mountain array is impossible.
Constraints:
1 <= n == nums.length <= 10001 <= nums[i] <= 1000
Solution
Approach: Prefix and Suffix DP
If n < 3 there is no valid peak, so return -1. Otherwise we choose a final value b[i] for every position, and pay |nums[i] - b[i]|. The final array must be strictly increasing up to the peak and strictly decreasing after it, with all values at least 1. No final value needs to exceed max(nums) + n, so call that bound V.
Let left[i][v] be the cheapest cost of turning nums[0..i] into a strictly increasing sequence that ends with value v:
left[i][v] = |nums[i] - v| + min(left[i-1][u] for u < v)
The inner minimum is a running prefix minimum, so each row costs O(V). The mirror table for the suffix, right[i][v], is built from the right in the same way (strictly decreasing from v down to the end).
A peak at index i with value v costs left[i][v] + right[i][v] - |nums[i] - v|, since both tables count the peak itself. The answer is the minimum over 0 < i < n - 1 and all v. The right table needs only the previous row, so it can be combined with left on the fly.
Complexity: O(n * (n + max(nums))) time and the same amount of space for the left table.
from itertools import accumulate
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
if n < 3:
return -1
V = max(nums) + n
INF = 10**15
def step(prev, x):
# cur[v] = |x - v| + min(prev[u] for u < v)
if prev is None:
return [INF] + [abs(x - v) for v in range(1, V + 1)]
pm = list(accumulate(prev, min))
return [INF, INF] + [pm[v - 1] + abs(x - v) for v in range(2, V + 1)]
# left[i][v]: cheapest way to make nums[0..i] strictly increasing and end at v
left = []
cur = None
for x in nums:
cur = step(cur, x)
left.append(cur)
# right pass: cur[v] is the same for nums[i..n-1] strictly decreasing from v
ans = INF
cur = None
for i in range(n - 1, -1, -1):
cur = step(cur, nums[i])
if 0 < i < n - 1:
for v in range(1, V + 1):
ans = min(ans, left[i][v] + cur[v] - abs(nums[i] - v))
return ans