File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ string longestDiverseString (int a, int b, int c) {
4+ priority_queue<pair<int , char >> pq;
5+ if (a != 0 ) pq.push ({ a, ' a' });
6+ if (b != 0 ) pq.push ({ b, ' b' });
7+ if (c != 0 ) pq.push ({ c, ' c' });
8+ char prev1 = ' #' ;
9+ char prev2 = ' #' ;
10+ string res;
11+ while (!pq.empty ())
12+ {
13+ auto [cnt1, ch1] = pq.top (); pq.pop ();
14+ if (ch1 == prev1 && ch1 == prev2)
15+ {
16+ if (pq.empty ()) return res;
17+ auto [cnt2, ch2] = pq.top (); pq.pop ();
18+ res += ch2;
19+ prev1 = prev2;
20+ prev2 = ch2;
21+ pq.push ({ cnt1, ch1 });
22+ if (--cnt2 > 0 ) pq.push ({ cnt2, ch2 });
23+ }
24+ else
25+ {
26+ prev1 = prev2;
27+ prev2 = ch1;
28+ res += ch1;
29+ if (--cnt1 > 0 ) pq.push ({ cnt1, ch1 });
30+ }
31+ }
32+ return res;
33+ }
34+ };
You can’t perform that action at this time.
0 commit comments