-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSolution.cpp
39 lines (37 loc) · 1.01 KB
/
Solution.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
// https://leetcode.com/problems/maximum-elegance-of-a-k-length-subsequence/
class Solution {
public:
long long findMaximumElegance(vector<vector<int>>& items, int k) {
int n = items.size();
vector<int> indices(n);
iota(indices.begin(), indices.end(), 0);
sort(indices.begin(), indices.end(),
[&](int i, int j) { return items[i][0] > items[j][0]; });
unordered_set<int> seen;
long long tot{}, ret{}, cats{};
multiset<int> lowest;
for (int i = 0; i < n; ++i) {
auto item = items[indices[i]];
int p = item[0], c = item[1];
if (i < k) {
tot += p;
if (!seen.count(c)) {
++cats;
seen.insert(c);
} else {
lowest.insert(p);
}
} else {
if (lowest.empty()) break;
if (!seen.count(c)) {
++cats;
seen.insert(c);
tot += p - *lowest.begin();
lowest.erase(lowest.begin());
}
}
ret = max(ret, tot + cats * cats);
}
return ret;
}
};