-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1405.cpp
More file actions
31 lines (29 loc) · 896 Bytes
/
Copy path1405.cpp
File metadata and controls
31 lines (29 loc) · 896 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
class Solution {
typedef tuple<int, char> record;
public:
string longestDiverseString(int a, int b, int c) const {
priority_queue<record> pq;
if (a > 0) pq.emplace(a, 'a');
if (b > 0) pq.emplace(b, 'b');
if (c > 0) pq.emplace(c, 'c');
string res;
int last = '!', lastc = 1;
while (!pq.empty()) {
const auto [cnt, c] = pq.top();
pq.pop();
if (c == last && lastc == 2) {
if (pq.empty()) return res;
const auto [ocnt, oc] = pq.top();
pq.pop();
if (ocnt > 1) pq.emplace(ocnt - 1, oc);
res += oc;
lastc = 0;
}
if (c != last) last = c, lastc = 0;
if (cnt > 1) pq.emplace(cnt - 1, c);
res += c;
lastc++;
}
return res;
}
};