leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1405.cpp (896B)
0 class Solution {
1 typedef tuple<int, char> record;
3 public:
4 string longestDiverseString(int a, int b, int c) const {
5 priority_queue<record> pq;
6 if (a > 0) pq.emplace(a, 'a');
7 if (b > 0) pq.emplace(b, 'b');
8 if (c > 0) pq.emplace(c, 'c');
10 string res;
11 int last = '!', lastc = 1;
12 while (!pq.empty()) {
13 const auto [cnt, c] = pq.top();
14 pq.pop();
15 if (c == last && lastc == 2) {
16 if (pq.empty()) return res;
17 const auto [ocnt, oc] = pq.top();
18 pq.pop();
19 if (ocnt > 1) pq.emplace(ocnt - 1, oc);
20 res += oc;
21 lastc = 0;
22 }
23 if (c != last) last = c, lastc = 0;
24 if (cnt > 1) pq.emplace(cnt - 1, c);
25 res += c;
26 lastc++;
27 }
28 return res;
29 }
30 };