-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1887.cpp
79 lines (70 loc) · 1.57 KB
/
p1887.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int findFirstOccurrence(vector<int> &nums, int start, int end, int key)
{
int ans = -1;
while (start <= end)
{
int mid = (start + end) / 2;
if (nums[mid] == key)
{
ans = mid;
end = mid - 1;
}
else if (nums[mid] < key)
{
start = mid + 1;
}
else if (nums[mid] > key)
{
end = mid - 1;
}
}
return ans;
}
public:
int reductionOperations(vector<int> &nums)
{
int n = nums.size();
int ans = 0;
sort(nums.begin(), nums.end());
int prev = 0;
int cnt = 1;
for (int i = n - 2; i >= 0; i--)
{
if (nums[i] != nums[i + 1])
{
ans += cnt + prev;
prev += cnt;
cnt = 1;
}
else
{
cnt++;
}
}
// int count = 0;
// int i = n;
// int largest = nums[n - 1];
// while (i > 0)
// {
// i = findFirstOccurrence(nums, 0, i - 1, largest);
// if (i > 0)
// {
// count += n - i;
// largest = nums[i - 1];
// }
// };
return ans;
}
};
int main()
{
vector<int> v = {1, 1, 2, 2, 3, 3, 3};
int ans = Solution().reductionOperations(v);
cout << ans;
return 0;
}