Skip to content

Commit df97977

Browse files
authored
Merge pull request #40 from Yash-Bandal/main
Added the potd October 16 With DryRun
2 parents 57152a5 + 86c2897 commit df97977

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

october_2024/potd_16_10_2024.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//1405. Longest Happy String
2+
3+
#include <iostream>
4+
#include <queue>
5+
#include <string>
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
string longestDiverseString(int a, int b, int c) {
11+
priority_queue<pair<int, char>> pq;
12+
13+
// Push in pq pairs of count and character
14+
if (a > 0) {
15+
pq.push({a, 'a'}); // Example input: a = 1, b = 1, c = 7
16+
}
17+
if (b > 0) {
18+
pq.push({b, 'b'});
19+
}
20+
if (c > 0) {
21+
pq.push({c, 'c'});
22+
}
23+
24+
// Don't declare vector<string> silly mistake
25+
string res = "";
26+
27+
// Dry run starts here
28+
/*
29+
Initial priority queue state:
30+
pq = [(7, 'c'), (1, 'a'), (1, 'b')]
31+
32+
Result string starts as empty: res = ""
33+
*/
34+
35+
while (!pq.empty()) {
36+
auto [count1, char1] = pq.top(); // Get the character with the highest count
37+
pq.pop();
38+
39+
// Check the 'three same characters constraint'
40+
// last second last
41+
if (res.size() >= 2 && res.back() == char1 && res[res.size() - 2] == char1) {
42+
// Condition is met, so we can't add char1
43+
if (pq.empty()) {
44+
break; // No other characters available
45+
}
46+
// Take next most frequent character
47+
auto [count2, char2] = pq.top(); // Get the second most frequent character
48+
pq.pop();
49+
50+
res += char2; // Add char2 to the result
51+
count2--; // Decrement its count
52+
53+
// Check if we can push char2 back into the queue
54+
if (count2 > 0) {
55+
pq.push({count2, char2});
56+
}
57+
58+
// Push char1 back into the queue as well
59+
pq.push({count1, char1});
60+
} else {
61+
// If adding char1 is okay
62+
res += char1; // Add char1 to the result
63+
count1--; // Decrement its count
64+
65+
// Push char1 back if there's still some left
66+
if (count1 > 0) {
67+
pq.push({count1, char1});
68+
}
69+
}
70+
}
71+
72+
return res; // Final result will be "ccaccbcc" or similar valid output
73+
}
74+
};
75+
// Dry run example updates:
76+
/*
77+
1st iteration:
78+
count1 = 7, char1 = 'c', res = "c"
79+
count1 decremented to 6. pq = [(6, 'c'), (1, 'a'), (1, 'b')]
80+
81+
2nd iteration:
82+
count1 = 6, char1 = 'c', res = "cc" (not allowed)
83+
count2 = 1, char2 = 'a', res = "cca", count2 decremented to 0. pq = [(6, 'c'),
84+
(1, 'b')]
85+
86+
3rd iteration:
87+
count1 = 6, char1 = 'c', res = "ccac"
88+
count1 decremented to 5. pq = [(5, 'c'), (1, 'b')]
89+
90+
4th iteration:
91+
count1 = 5, char1 = 'c', res = "ccaca" (not allowed)
92+
count2 = 1, char2 = 'b', res = "ccacb", count2 decremented to 0. pq = [(5, 'c')]
93+
94+
5th iteration:
95+
count1 = 5, char1 = 'c', res = "ccacbc"
96+
count1 decremented to 4. pq = [(4, 'c')]
97+
98+
6th iteration:
99+
count1 = 4, char1 = 'c', res = "ccacbcc"
100+
count1 decremented to 3. pq = [(3, 'c')]
101+
102+
7th iteration:
103+
count1 = 3, char1 = 'c', res = "ccacbcc"
104+
...
105+
Continue until the priority queue is empty.
106+
*/

0 commit comments

Comments
 (0)