-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1090.cpp
More file actions
27 lines (24 loc) · 794 Bytes
/
1090.cpp
File metadata and controls
27 lines (24 loc) · 794 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
class Solution {
public:
int largestValsFromLabels(vector<int>& values, vector<int>& labels, int numWanted, int useLimit) { //O(N log N)
vector<pair<int,int>> v;
for(int i = 0; i < values.size(); i++){
v.push_back({values[i], labels[i]});
}
auto cmp = [](pair<int,int>& a, pair<int,int>& b){
return a.first > b.first;
};
sort(v.begin(), v.end(), cmp);
int val = 0; int num = 0;
unordered_map<int,int> m;
for(int i = 0; i < v.size(); i++){
if(m[v[i].second] < useLimit){
m[v[i].second]++;
val += v[i].first;
num++;
}
if(num == numWanted) break;
}
return val;
}
};