-
Notifications
You must be signed in to change notification settings - Fork 3
/
1405.cpp
31 lines (29 loc) · 921 Bytes
/
1405.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
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
priority_queue<pair<int, char>> pq;
if (a != 0) pq.push({a, 'a'});
if (b != 0) pq.push({b, 'b'});
if (c != 0) pq.push({c, 'c'});
string res;
while (!pq.empty()) {
if (pq.size() == 1) {
int k = min(2, pq.top().first);
for (int i = 0; i < k; ++i) res.push_back(pq.top().second);
return res;
}
auto x = pq.top();
pq.pop();
auto y = pq.top();
pq.pop();
int k = min(2, x.first - y.first);
for (int i = 0; i < k; ++i) res.push_back(x.second);
res.push_back(y.second);
x.first -= k;
y.first -= 1;
if (x.first > 0) pq.push(x);
if (y.first > 0) pq.push(y);
}
return res;
}
};