generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
25 lines (22 loc) · 846 Bytes
/
solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// The solution can be done simply by counting the frequencies and then
// iterating through the frequencies from largest to smallest to count the required number of operations.
int reductionOperations(int* nums, int numsSize) {
// Count the frequencies and find the minimum and maximum numbers.
int frequencies[50001] = {0};
int maxNum = 1;
int minNum = 5000;
for (int i = 0; i < numsSize; ++i) {
++frequencies[nums[i]];
if (nums[i] > maxNum) maxNum = nums[i];
if (nums[i] < minNum) minNum = nums[i];
}
// Iterating through the frequencies to count the required number of operations.
int frequencySum = 0;
int operations = 0;
for (int num = maxNum; num > minNum; --num) {
if (frequencies[num] == 0) continue;
frequencySum += frequencies[num];
operations += frequencySum;
}
return operations;
}