Loading...
You are given an integer array nums.
Consider an operation where you choose two distinct values x and y that currently appear in the array and replace every element equal to x with y. The operation costs the number of elements that were changed.
Repeat this operation until all elements are equal. Return the minimum total cost.
Input: nums = [7, 7, 5, 7, 3, 5, 3]
Output: 4
Explanation: Replace 5 -> 7 (two elements, cost 2), giving [7, 7, 7, 7, 3, 7, 3]; then 3 -> 7 (two elements, cost 2). Total 4.
Input: nums = [4, 4, 4]
Output: 0
Explanation: All elements are already equal.
Input: nums = [1, 2, 3]
Output: 2
Explanation: Any two of the three distinct values must change, one element each.
nums.length ≤105nums[i] ≤109Click "Run" to test with sample cases or "Submit" to run all tests.