🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gR-ZeuhX 💡 My thought process: The approach involves checking all possible valid times that can show on a binary watch. We select the times where the number of LEDs turned on matches the given value. A binary watch displays hours from 0 to 11 and minutes from 0 to 59, so we look at every possible hour and minute combination. For each pair, we count how many bits are set to 1 in the binary representation of the hour and the minute using the built-in `__builtin_popcount()` function. If the total number of set bits in the hour and minute equals the input `turnedOn`, that time is valid. When creating the time string, we add the hour normally, but the minute must always be in two-digit format. A leading zero is added when the minute value is less than 10. We store all valid formatted times in a result list and return it at the end. 👉 My Solution: https://lnkd.in/gN6FMYtF If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
LeetCode Challenge: Binary Watch Problem Solution
More Relevant Posts
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g5nYsHBR 💡 My thought process: To form a decimal number n using the fewest deci-binary numbers, the key factor is the highest digit in n. Each deci-binary number can contribute a maximum of 1 to any digit position. So, if a digit in n is 7, we need at least 7 deci-binary numbers to reach that sum. Thus, the minimum number of deci-binary numbers needed is simply the highest digit in the provided string. The approach is simple: start with a variable result set to 0. Go through each character in the string, convert it to an integer using n[i] - '0', update result with the greater value between result and digit, and finally return result. 👉 My Solution: https://lnkd.in/ggv3b48t If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g5ggMZxw 💡 My thought process: The approach checks how many mismatches occur if the string follows an alternating pattern starting with 0 (0,1,0,1...), counts the number of positions that violate this pattern, and since the opposite alternating pattern starting with 1 (1,0,1,0...) would require the remaining changes, the minimum of the two mismatch counts represents the minimum number of operations required. 👉 My Solution: https://lnkd.in/gVuWTeKh If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g6uRPkE6 💡 My thought process: The first approach calculates the bitwise complement by examining each bit of the number. It starts by determining how many bits are needed to represent n using log2(n) + 1. For each bit position i, a mask (1 << i) is used to isolate that bit from n. If the extracted bit is 0, the result for that position is set by adding the same mask, effectively flipping the bit. This creates a new number where all bits within the effective bit length of n are inverted, resulting in the complement. The second approach uses the XOR operation to flip all relevant bits at once. It first calculates the number of bits in n and creates a mask filled with 1s in all those positions using (1 << digits) - 1. This mask represents the highest value that can be formed with the same number of bits. Applying n ^ mask flips every bit within that range because XOR with 1 changes the bit and XOR with 0 leaves it the same, giving the bitwise complement of n. 👉 My Solution: https://lnkd.in/gGCdUW3H If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gTrw45pn 💡 My thought process: We check each bit one by one and compare it with the previous bit. We start with prevBit initialized to -1 which means we haven't seen any bit yet. We loop until n becomes 0 and in each iteration we get the rightmost bit using bitwise AND with 1. If the current bit equals the previous bit then we immediately return false because the bits are not alternating. Otherwise we save the current bit as the previous bit for the next comparison and remove the rightmost bit by right shifting n by 1 position. If we complete the entire loop without finding two consecutive same bits then we return true indicating all bits are alternating. 👉 My Solution: https://lnkd.in/gQpKAMSi If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/geeKcuMP 💡 My thought process: The approach counts the number of special positions in a binary matrix by ensuring that a cell containing 1 is the only 1 in both its row and column. First, the number of rows, m, and columns, n, are determined. Two auxiliary arrays, rowOnes and colOnes, store the count of 1s in each row and column, respectively. The matrix is first traversed row by row to count how many 1s appear in each row and store these counts in rowOnes. Next, it is traversed column by column to count how many 1s appear in each column and store these counts in colOnes. After computing these counts, the matrix is scanned again. For every cell containing 1, it checks whether that row and column both contain exactly one 1. If both conditions are met, the position is considered special, and the count is increased. Finally, the total number of such positions is returned. 👉 My Solution: https://lnkd.in/g9eZdbF2 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g4J85qey 💡 My thought process: If you have a block of three 0s followed by a block of two 1s (00011), you can only create two valid pairs: 01 and 0011. The smaller group always limits the number of matches you can make, which is why the code uses min(prev, curr). To keep track of this, the code uses prev for the size of the block you just finished and curr for the block you’re currently counting. As you go through the string, you keep increasing curr as long as the characters remain the same. The moment the character changes, like when you hit a 1 after a series of 0s, it means you've completed a block. You take the min of the old block and the new block, add it to your total, move the curr count into prev, and start over for the next group. There is one small issue in the code: because that addition only happens when the characters change, the very last block in the string doesn't get processed inside the loop. That’s why there is one extra result += min(prev, curr) at the very end to catch that final transition. 👉 My Solution: https://lnkd.in/gdK35f76 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gDzrB9cW 💡 My thought process: This solution creates the binary string step by step, starting from "0". For each level from 2 to n, it forms a new string by taking the previous string, inserting "1" in the middle, and then adding the reversed and bit-flipped version of the previous string (0 changes to 1 and 1 changes to 0). After each construction, it checks if the string has reached or exceeded the required k position. If it has, it immediately returns the k-th character. This approach prevents any extra construction once the answer is found. 👉 My Solution: https://lnkd.in/gFXgkTpm If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gx6PQVMB 💡 My thought process: The brute force method checks all possible rotations of the binary string by moving the first character to the end repeatedly. For each rotated string, it counts the number of mismatches needed to create an alternating pattern that starts with 1. The opposite alternating pattern, which starts with 0, would require different changes. The method takes the minimum of the two counts for each rotation and returns the lowest value overall. The optimized method does not generate all rotations. Instead, it doubles the string and uses a sliding window of size n to represent every possible rotation. It keeps track of the mismatch count for forming an alternating pattern that starts with 1. As the window slides, it updates this count by subtracting the contribution of the character that leaves the window and adding the incoming character. At each step, it compares both alternating patterns to find the minimum number of flips needed. 👉 My Solution: https://lnkd.in/gP2FWVYP If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/grtpbGeG 💡 My thought process: A counter tracks the number of operations. The loop runs until the string length becomes 1, which means the number has been reduced to 1. If the last character of the string is 0, the number is even in binary. Dividing by 2 is the same as removing the last bit, so pop_back removes the trailing 0. If the last character is 1, the number is odd and needs to be increased by 1. To simulate binary addition, the code goes through the string from right to left. All consecutive trailing 1 characters are changed to 0 to manage the carry. When a 0 is found, it becomes 1, and the carry stops. If the loop finishes without finding a 0, it means the string was made entirely of 1 characters. Adding 1 then results in an extra leading 1, which is added by putting 1 at the front of the string. Each iteration counts as one step. This process continues until only one character is left. The function finally returns the total number of steps needed to reduce the binary number to 1. 👉 My Solution: https://lnkd.in/guc6fia6 If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 40 𝗼𝗳 𝟯𝟲𝟬 days of LeetCode Challenge. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 :- 3714. Longest Balanced Substring II You are given a string s consisting only of the characters 'a', 'b', and 'c'. A substring of s is called balanced if all distinct characters in the substring appear the same number of times. Return the length of the longest balanced substring of s. 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗹𝗶𝗻𝗸:- https://lnkd.in/dqJiZzXm 𝗛𝗶𝗻𝘁:- 1. Break the problem by number of distinct characters: handle single-character runs, then for two characters use prefix-difference (count₁ − count₂) with a hashmap, and for three characters track two independent differences. 2. Whenever the same difference state reappears, the substring between those indices has equal counts, so update the maximum length. 𝗚𝗶𝘁𝗵𝘂𝗯 𝗰𝗼𝗱𝗲:- https://lnkd.in/dHNThed3 #SoftwareEngineering #DSAJourney #Coding #LeetCode #DSA #TechCareers #SDE
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development