Description
Submission
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
typedef pair<int, char> PIC;
set<PIC, greater<>> Set;
if(a)
Set.insert({a, 'a'});
if(b)
Set.insert({b, 'b'});
if(c)
Set.insert({c, 'c'});
string ret = "";
while(!Set.empty()) {
auto iter = Set.begin();
for(; iter != Set.end(); ++iter) {
if(!ret.empty() && iter->second == ret.back()) continue;
if(iter == Set.begin()) {
if(iter->first >= 2) {
PIC p {iter->first - 2, iter->second};
Set.erase(iter);
ret.push_back(p.second);
ret.push_back(p.second);
if(p.first != 0)
Set.insert(p);
} else {
ret.push_back(iter->second);
Set.erase(iter);
}
} else {
PIC p {iter->first - 1, iter->second};
Set.erase(iter);
ret.push_back(p.second);
if(p.first != 0)
Set.insert(p);
}
break;
}
if(Set.size() == 1 && Set.begin()->second == ret.back()) break;
}
return ret;
}
};
Submission 210811
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
typedef pair<int, char> PIC;
priority_queue<PIC, vector<PIC>, less<>> pq;
if(a) pq.push({a, 'a'});
if(b) pq.push({b, 'b'});
if(c) pq.push({c, 'c'});
string ret = "";
while(!pq.empty()) {
if(pq.size() == 1) {
for(int i = 0; i < min(pq.top().first, 2); ++i) {
ret.push_back(pq.top().second);
}
return ret;
}
auto [cnt1, ch1] = pq.top();
pq.pop();
auto [cnt2, ch2] = pq.top();
pq.pop();
int k = min(cnt1 - cnt2 + 1, 2);
cnt1 -= k;
while(k--) {
ret.push_back(ch1);
}
cnt2 -= 1;
ret.push_back(ch2);
if(cnt1) pq.push({cnt1, ch1});
if(cnt2) pq.push({cnt2, ch2});
}
return ret;
}
};