-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1405_Longest-Happy-String.cpp
More file actions
55 lines (51 loc) · 1.48 KB
/
1405_Longest-Happy-String.cpp
File metadata and controls
55 lines (51 loc) · 1.48 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <queue>
#include <string>
#include <vector>
using namespace std;
class Solution
{
public:
string longestDiverseString(int a, int b, int c)
{
int l = a + b + c;
priority_queue<pair<int, char>> pq;
pq.push(make_pair(a, 'a'));
pq.push(make_pair(b, 'b'));
pq.push(make_pair(c, 'c'));
string ans = "";
vector<int> cnt(3);
while (pq.top().first > 0)
{
auto t = pq.top();
pq.pop();
if (cnt[t.second - 'a'] == 2 && ans.back() == t.second)
{
auto t2 = pq.top();
pq.pop();
if (t2.first == 0)
{
break;
}
ans += t2.second;
t2.first--;
cnt[0] = cnt[1] = cnt[2] = 0;
cnt[t2.second - 'a']++;
pq.push(t2);
pq.push(t);
continue;
}
if (!ans.empty() && ans.back() != t.second)
{
cnt[0] = cnt[1] = cnt[2] = 0;
}
ans += t.second;
t.first--;
cnt[t.second - 'a']++;
pq.push(t);
}
return ans;
}
};
// 还是用堆储存,但这次有三个字母,其他没啥区别,还是先放最大的字母,
// 若是达到两个,然后下一个加入的也会是该字母,换下一个最大的字母加入
// 新知道了个cnt[0] = cnt[1] = cnt[2] = 0