-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1405. Longest Happy String.cpp
54 lines (54 loc) · 1.36 KB
/
1405. Longest Happy String.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
priority_queue<pair<int,char>,vector<pair<int,char>>>pq;
if(a){
pq.push({a,'a'});
}
if(b){
pq.push({b,'b'});
}
if(c){
pq.push({c,'c'});
}
string ans="";
char last='e';
while(!pq.empty()){
int maxi=pq.top().first;
char c=pq.top().second;
pq.pop();
if(last!=c){
last=c;
int x=min(2,maxi);
maxi-=x;
while(x--){
ans.push_back(c);
}
if(maxi){
pq.push({maxi,c});
}
}
else{
if(pq.empty()){
return ans;
}
int newMaxi=pq.top().first;
char newC=pq.top().second;
pq.pop();
last=newC;
int x=min(1,newMaxi);
newMaxi-=x;
while(x--){
ans.push_back(newC);
}
if(newMaxi){
pq.push({newMaxi,newC});
}
if(maxi){
pq.push({maxi,c});
}
}
}
return ans;
}
};