forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-happy-string.cpp
72 lines (70 loc) · 2.07 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Time: O(n)
// Space: O(1)
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
priority_queue<pair<int, char>> max_heap;
if (a) {
max_heap.emplace(a, 'a');
}
if (b) {
max_heap.emplace(b, 'b');
}
if (c) {
max_heap.emplace(c, 'c');
}
string result;
while (!max_heap.empty()) {
auto [count1, c1] = max_heap.top(); max_heap.pop();
if (result.length() >= 2 &&
result[result.length() - 2] == c1 &&
result[result.length() - 1] == c1) {
if (max_heap.empty()) {
return result;
}
auto [count2, c2] = max_heap.top(); max_heap.pop();
result.push_back(c2);
--count2;
if (count2) {
max_heap.emplace(count2, c2);
}
max_heap.emplace(count1, c1);
continue;
}
result.push_back(c1);
--count1;
if (count1) {
max_heap.emplace(count1, c1);
}
}
return result;
}
};
// Time: O(n)
// Space: O(1)
class Solution2 {
public:
string longestDiverseString(int a, int b, int c) {
vector<pair<int, char>> choices = {{a, 'a'}, {b, 'b'}, {c, 'c'}};
string result;
for (int i = 0; i < a + b + c; ++i) {
sort(begin(choices), end(choices), greater<pair<int, int>>());
bool is_found = false;
for (auto& kvp :choices) {
if (kvp.first &&
(result.length() < 2 ||
result[result.length() - 2] != kvp.second ||
result[result.length() - 1] != kvp.second)) {
result.push_back(kvp.second);
--kvp.first;
is_found = true;
break;
}
}
if (!is_found) {
break;
}
}
return result;
}
};