forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (33 loc) · 1.03 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
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
using pci = pair<char, int>;
auto cmp = [](pci x, pci y) { return x.second < y.second; };
priority_queue<pci, vector<pci>, decltype(cmp)> pq(cmp);
if (a > 0) pq.push({'a', a});
if (b > 0) pq.push({'b', b});
if (c > 0) pq.push({'c', c});
string ans;
while (!pq.empty()) {
pci cur = pq.top();
pq.pop();
int n = ans.size();
if (n >= 2 && ans[n - 1] == cur.first && ans[n - 2] == cur.first) {
if (pq.empty()) break;
pci nxt = pq.top();
pq.pop();
ans.push_back(nxt.first);
if (--nxt.second > 0) {
pq.push(nxt);
}
pq.push(cur);
} else {
ans.push_back(cur.first);
if (--cur.second > 0) {
pq.push(cur);
}
}
}
return ans;
}
};