-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1405.cpp
More file actions
36 lines (36 loc) · 1.01 KB
/
Copy path1405.cpp
File metadata and controls
36 lines (36 loc) · 1.01 KB
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) {
string res = "##";
int n = a+b+c;
priority_queue<pair<int,int>> pq;
if(a>0)
pq.push({a,0});
if(b>0)
pq.push({b,1});
if(c>0)
pq.push({c,2});
while(!pq.empty()){
auto curr = pq.top();pq.pop();
char ch = curr.second + 'a';
if(res[res.size()-1] == ch && res[res.size()-2] == ch){
if(pq.empty())
return res.substr(2);
auto curr2 = pq.top();pq.pop();
pq.push(curr);
ch = curr2.second + 'a';
res += ch;
curr2.first -= 1;
if(curr2.first>0)
pq.push(curr2);
}
else{
res += ch;
curr.first -= 1;
if(curr.first>0)
pq.push(curr);
}
}
return res.substr(2);
}
};