Loading...
Loading...
You are given a list of positive integers nums, a list targets of exactly three positive integers, and a positive integer mergeCost.
You can change nums with two kinds of operations:
1 to one element, or subtract 1 from it. This costs 1.mergeCost.The goal is for nums to contain three separate elements whose values are targets[0], targets[1] and targets[2], in any order. Every other element can stay as it is, so you don't have to use all of nums.
Return the minimum total cost to reach the goal.
Input: nums = [98, 40, 30, 21, 80], targets = [100, 90, 80], mergeCost = 10
Output: 23
Explanation: Merge 40 and 30 into 70 (cost 10), then merge 70 and 21 into 91 (cost 10).
Subtract 1 from 91 to get 90 (cost 1). Add 1 to 98 twice to get 100 (cost 2).
80 already matches. The total is 10 + 10 + 1 + 2 = 23.
Input: nums = [1, 1, 1], targets = [5, 1, 1], mergeCost = 7
Output: 4
Explanation: Add 1 to one of the 1s four times to get 5 (cost 4).
The other two 1s already match. Any merge would cost 7 on its own, which is more than 4.
Input: nums = [3, 4, 5, 6], targets = [7, 5, 6], mergeCost = 2
Output: 2
Explanation: Merge 3 and 4 into 7 (cost 2). 5 and 6 already match.
Adjusting instead (6 to 7, 5 to 6 and 4 to 5) would cost 3.
Input: nums = [2, 9, 50, 4, 6], targets = [6, 10, 4], mergeCost = 3
Output: 1
Explanation: 6 and 4 already match. Add 1 to 9 to get 10 (cost 1).
2 and 50 are not used, and they stay in nums.
nums.length ≤10nums[i] ≤106targets.length =3targets[i] ≤106mergeCost ≤106