Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 362ffe1

Browse files
feat: add cpp solution to lc problem: No.1405
No.1405.Longest Happy String
1 parent 6a793d7 commit 362ffe1

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

‎solution/1400-1499/1405.Longest Happy String/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,47 @@ func longestDiverseString(a int, b int, c int) string {
225225
}
226226
```
227227

228+
### **C++**
229+
230+
```cpp
231+
class Solution {
232+
public:
233+
string longestDiverseString(int a, int b, int c) {
234+
using pci = pair<char, int>;
235+
auto cmp = [](pci x, pci y) { return x.second < y.second; };
236+
priority_queue<pci, vector<pci>, decltype(cmp)> pq(cmp);
237+
238+
if (a > 0) pq.push({'a', a});
239+
if (b > 0) pq.push({'b', b});
240+
if (c > 0) pq.push({'c', c});
241+
242+
string ans;
243+
while (!pq.empty()) {
244+
pci cur = pq.top();
245+
pq.pop();
246+
int n = ans.size();
247+
if (n >= 2 && ans[n - 1] == cur.first && ans[n - 2] == cur.first) {
248+
if (pq.empty()) break;
249+
pci nxt = pq.top();
250+
pq.pop();
251+
ans.push_back(nxt.first);
252+
if (--nxt.second > 0) {
253+
pq.push(nxt);
254+
}
255+
pq.push(cur);
256+
} else {
257+
ans.push_back(cur.first);
258+
if (--cur.second > 0) {
259+
pq.push(cur);
260+
}
261+
}
262+
}
263+
264+
return ans;
265+
}
266+
};
267+
```
268+
228269
### **...**
229270

230271
```

‎solution/1400-1499/1405.Longest Happy String/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,47 @@ func longestDiverseString(a int, b int, c int) string {
217217
}
218218
```
219219

220+
### **C++**
221+
222+
```cpp
223+
class Solution {
224+
public:
225+
string longestDiverseString(int a, int b, int c) {
226+
using pci = pair<char, int>;
227+
auto cmp = [](pci x, pci y) { return x.second < y.second; };
228+
priority_queue<pci, vector<pci>, decltype(cmp)> pq(cmp);
229+
230+
if (a > 0) pq.push({'a', a});
231+
if (b > 0) pq.push({'b', b});
232+
if (c > 0) pq.push({'c', c});
233+
234+
string ans;
235+
while (!pq.empty()) {
236+
pci cur = pq.top();
237+
pq.pop();
238+
int n = ans.size();
239+
if (n >= 2 && ans[n - 1] == cur.first && ans[n - 2] == cur.first) {
240+
if (pq.empty()) break;
241+
pci nxt = pq.top();
242+
pq.pop();
243+
ans.push_back(nxt.first);
244+
if (--nxt.second > 0) {
245+
pq.push(nxt);
246+
}
247+
pq.push(cur);
248+
} else {
249+
ans.push_back(cur.first);
250+
if (--cur.second > 0) {
251+
pq.push(cur);
252+
}
253+
}
254+
}
255+
256+
return ans;
257+
}
258+
};
259+
```
260+
220261
### **...**
221262

222263
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string longestDiverseString(int a, int b, int c) {
4+
using pci = pair<char, int>;
5+
auto cmp = [](pci x, pci y) { return x.second < y.second; };
6+
priority_queue<pci, vector<pci>, decltype(cmp)> pq(cmp);
7+
8+
if (a > 0) pq.push({'a', a});
9+
if (b > 0) pq.push({'b', b});
10+
if (c > 0) pq.push({'c', c});
11+
12+
string ans;
13+
while (!pq.empty()) {
14+
pci cur = pq.top();
15+
pq.pop();
16+
int n = ans.size();
17+
if (n >= 2 && ans[n - 1] == cur.first && ans[n - 2] == cur.first) {
18+
if (pq.empty()) break;
19+
pci nxt = pq.top();
20+
pq.pop();
21+
ans.push_back(nxt.first);
22+
if (--nxt.second > 0) {
23+
pq.push(nxt);
24+
}
25+
pq.push(cur);
26+
} else {
27+
ans.push_back(cur.first);
28+
if (--cur.second > 0) {
29+
pq.push(cur);
30+
}
31+
}
32+
}
33+
34+
return ans;
35+
}
36+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /