From 86c2897b1bac190a1e652939d795a31c3508f11f Mon Sep 17 00:00:00 2001 From: Yash Bandal <147707204+Yash-Bandal@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:00:49 +0530 Subject: [PATCH] Added the potd October 16 With DryRun --- october_2024/potd_16_10_2024.cpp | 106 +++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 october_2024/potd_16_10_2024.cpp diff --git a/october_2024/potd_16_10_2024.cpp b/october_2024/potd_16_10_2024.cpp new file mode 100644 index 0000000..899a087 --- /dev/null +++ b/october_2024/potd_16_10_2024.cpp @@ -0,0 +1,106 @@ +//1405. Longest Happy String + +#include +#include +#include +using namespace std; + +class Solution { +public: + string longestDiverseString(int a, int b, int c) { + priority_queue> pq; + + // Push in pq pairs of count and character + if (a > 0) { + pq.push({a, 'a'}); // Example input: a = 1, b = 1, c = 7 + } + if (b > 0) { + pq.push({b, 'b'}); + } + if (c > 0) { + pq.push({c, 'c'}); + } + + // Don't declare vector silly mistake + string res = ""; + + // Dry run starts here + /* + Initial priority queue state: + pq = [(7, 'c'), (1, 'a'), (1, 'b')] + + Result string starts as empty: res = "" + */ + + while (!pq.empty()) { + auto [count1, char1] = pq.top(); // Get the character with the highest count + pq.pop(); + + // Check the 'three same characters constraint' + // last second last + if (res.size() >= 2 && res.back() == char1 && res[res.size() - 2] == char1) { + // Condition is met, so we can't add char1 + if (pq.empty()) { + break; // No other characters available + } + // Take next most frequent character + auto [count2, char2] = pq.top(); // Get the second most frequent character + pq.pop(); + + res += char2; // Add char2 to the result + count2--; // Decrement its count + + // Check if we can push char2 back into the queue + if (count2 > 0) { + pq.push({count2, char2}); + } + + // Push char1 back into the queue as well + pq.push({count1, char1}); + } else { + // If adding char1 is okay + res += char1; // Add char1 to the result + count1--; // Decrement its count + + // Push char1 back if there's still some left + if (count1 > 0) { + pq.push({count1, char1}); + } + } + } + + return res; // Final result will be "ccaccbcc" or similar valid output + } +}; +// Dry run example updates: +/* +1st iteration: +count1 = 7, char1 = 'c', res = "c" +count1 decremented to 6. pq = [(6, 'c'), (1, 'a'), (1, 'b')] + +2nd iteration: +count1 = 6, char1 = 'c', res = "cc" (not allowed) +count2 = 1, char2 = 'a', res = "cca", count2 decremented to 0. pq = [(6, 'c'), +(1, 'b')] + +3rd iteration: +count1 = 6, char1 = 'c', res = "ccac" +count1 decremented to 5. pq = [(5, 'c'), (1, 'b')] + +4th iteration: +count1 = 5, char1 = 'c', res = "ccaca" (not allowed) +count2 = 1, char2 = 'b', res = "ccacb", count2 decremented to 0. pq = [(5, 'c')] + +5th iteration: +count1 = 5, char1 = 'c', res = "ccacbc" +count1 decremented to 4. pq = [(4, 'c')] + +6th iteration: +count1 = 4, char1 = 'c', res = "ccacbcc" +count1 decremented to 3. pq = [(3, 'c')] + +7th iteration: +count1 = 3, char1 = 'c', res = "ccacbcc" +... +Continue until the priority queue is empty. +*/