Given an integer array nums, your goal is to make all elements in nums
equal. To complete one operation, follow these steps:
Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
Find the next largest value in numsstrictly smaller than largest. Let its value be nextLargest.
Reduce nums[i] to nextLargest.
Return the number of operations to make all elements innumsequal.
Input: nums =[5,1,3]Output: 3Explanation: It takes 3 operations to make all elements in nums equal:1. largest =5 at index 0. nextLargest =3. Reduce nums[0] to 3. nums =[_3_ ,1,3].2. largest =3 at index 0. nextLargest =1. Reduce nums[0] to 1. nums =[_1_ ,1,3].3. largest =3 at index 2. nextLargest =1. Reduce nums[2] to 1. nums =[1,1,_1_].
Input: nums =[1,1,2,2,3]Output: 4Explanation: It takes 4 operations to make all elements in nums equal:1. largest =3 at index 4. nextLargest =2. Reduce nums[4] to 2. nums =[1,1,2,2,_2_].2. largest =2 at index 2. nextLargest =1. Reduce nums[2] to 1. nums =[1,1,_1_ ,2,2].3. largest =2 at index 3. nextLargest =1. Reduce nums[3] to 1. nums =[1,1,1,_1_ ,2].4. largest =2 at index 4. nextLargest =1. Reduce nums[4] to 1. nums =[1,1,1,1,_1_].
Each time we reduce the largest value to the next largest, we perform an operation for every occurrence of the current largest. The total number of operations is the sum of the counts of all elements above the minimum, weighted by their position in the sorted unique list.
classSolution {
public:int reductionOperations(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans =0, cnt =0;
for (int i =1; i < nums.size(); ++i) {
if (nums[i] != nums[i-1]) ++cnt;
ans += cnt;
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
import"sort"funcreductionOperations(nums []int) int {
sort.Ints(nums)
ans, cnt:=0, 0fori:=1; i < len(nums); i++ {
ifnums[i] !=nums[i-1] {
cnt++ }
ans+=cnt }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;
classSolution {
publicintreductionOperations(int[] nums) {
Arrays.sort(nums);
int ans = 0, cnt = 0;
for (int i = 1; i < nums.length; ++i) {
if (nums[i]!= nums[i-1]) ++cnt;
ans += cnt;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funreductionOperations(nums: IntArray): Int {
nums.sort()
var ans = 0var cnt = 0for (i in1 until nums.size) {
if (nums[i] != nums[i-1]) cnt++ ans += cnt
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defreductionOperations(self, nums: list[int]) -> int:
nums.sort()
ans = cnt =0for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
cnt +=1 ans += cnt
return ans
1
2
3
4
5
6
7
8
9
10
11
12
impl Solution {
pubfnreduction_operations(mut nums: Vec<i32>) -> i32 {
nums.sort();
letmut ans =0;
letmut cnt =0;
for i in1..nums.len() {
if nums[i] != nums[i-1] { cnt +=1; }
ans += cnt;
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
reductionOperations(nums: number[]):number {
nums.sort((a, b) =>a-b);
letans=0, cnt=0;
for (leti=1; i<nums.length; ++i) {
if (nums[i] !==nums[i-1]) ++cnt;
ans+=cnt;
}
returnans;
}
}