-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1338.cpp
More file actions
40 lines (38 loc) · 895 Bytes
/
1338.cpp
File metadata and controls
40 lines (38 loc) · 895 Bytes
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
class Solution {
public:
static bool cp(pair<int,int>a,pair<int,int>b)
{
return a.first>b.first;
}
int minSetSize(vector<int>& arr) {
int n_want = arr.size()/2;
int n = arr.size();
unordered_map<int,int>ump;
for(int i=0;i<n;i++)
{
ump[arr[i]]++;
}
vector<pair<int,int>>pq;
for(auto it=ump.begin();it!=ump.end();it++)
{
pq.push_back(make_pair(it->second,it->first));
}
sort(pq.begin(),pq.end(),cp);
int i = 0;
int s = 0;
int len = pq[0].first;
int count = 1;
// [3,5,2,7]
while(s< n_want)
{
if(len==0)
{
count++;
len = pq[++i].first;
}
len--;
s++;
}
return count;
}
};