Minimum Cost to Make All Array Elements Equal Problem
Description
LeetCode Problem 4121.
You are given an integer array nums of length n.
You may perform any of the following operations any number of times, in any order.
- Prefix operation: Choose an index
isuch that0 < i < n - 1, and set all elements from index 0 toi - 1tonums[i]. The cost of this operation isi * nums[i]. - Suffix operation: Choose an index
isuch that0 < i < n - 1, and set all elements from indexi + 1ton - 1tonums[i]. The cost of this operation is(n - i - 1) * nums[i].
The cost is based on the current value of nums[i] and includes every element in the chosen prefix or suffix, even if it already equals nums[i].
Return the minimum total cost required to make all elements of nums equal.
Example 1:
Input: nums = [1,2,3]
Output: 4
Explanation:
One sequence of operations that achieves the minimum cost is as follows:
nums Operation i Modified nums Cost
[1, 2, 3] Suffix 1 [1, 2, 2] (3 - 1 - 1) * 2 = 2
[1, 2, 2] Prefix 1 [2, 2, 2] 1 * 2 = 2
Hence, the total cost is 2 + 2 = 4.
Example 2:
Input: nums = [3,3,3,6,3,3]
Output: 9
Explanation:
One sequence of operations that achieves the minimum cost is as follows:
nums Operation i Modified nums Cost
[3, 3, 3, 6, 3, 3] Suffix 2 [3, 3, 3, 3, 3, 3] (6 - 2 - 1) * 3 = 9
Hence, the total cost is 9.
Example 3:
Input: nums = [3,4,4,5,4,4,3]
Output: 20
Explanation:
One sequence of operations that achieves the minimum cost is as follows:
nums Operation i Modified nums Cost
[3, 4, 4, 5, 4, 4, 3] Prefix 4 [4, 4, 4, 4, 4, 4, 3] 4 * 4 = 16
[4, 4, 4, 4, 4, 4, 3] Suffix 5 [4, 4, 4, 4, 4, 4, 4] (7 - 5 - 1) * 4 = 4
Hence, the total cost is 16 + 4 = 20.
Constraints:
3 <= n == nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Equal Runs
A prefix operation at i overwrites everything to its left, and a suffix operation at i overwrites everything to its right. The value of the final array must therefore come from a run of equal values: pick a run [l, r] that holds x. Everything to the left of the run is covered by one prefix operation at l, and everything to the right by one suffix operation at r, which costs l * x and (n - r - 1) * x.
Together that is (l + n - r - 1) * x. The operation indices must satisfy 0 < i < n - 1, so a run only works when r > 0 and l < n - 1. If the run covers the whole array the cost is 0.
Scan nums once with two pointers: l is the start of the current run, and r moves forward. When r is the last index or nums[r] != nums[r + 1], the run [l, r] is complete, so evaluate its cost, then set l = r + 1. The answer is the smallest cost over all runs. Joining several runs of the same value never helps, because one prefix or suffix operation already pays for every position outside the chosen run.
Complexity: O(n) time and O(1) extra space.
class Solution:
def minCost(self, nums: List[int]) -> int:
n = len(nums)
ans = float('inf')
l = 0
for r, x in enumerate(nums):
if r == n - 1 or x != nums[r + 1]:
if r > 0 and l < n - 1:
ans = min(ans, (l + n - r - 1) * x)
l = r + 1
return ans