🚀 100 Days LeetCode Challenge – Day 37 Solved LeetCode 324: Wiggle Sort II ✅ Goal: Rearrange array such that: nums[0] < nums[1] > nums[2] < nums[3] > nums[4] ... 💡 Core Idea: Copy array into temp. Sort the temp array. Split sorted array into: Smaller half Larger half Fill: Even indices from smaller half (backwards) Odd indices from larger half (backwards) Filling the array backwards was important to properly handle duplicate elements. 🧠 Key Learnings: Pattern-based rearrangement problems often need: Sorting + smart indexing Reverse filling prevents equal elements from breaking wiggle condition. Even/Odd index control is a powerful technique. This was more about index manipulation strategy than brute logic. ⏱️ Complexity: Time: O(n log n) (due to sorting) Space: O(n) (temp array) #100DaysOfCode #LeetCode #WiggleSort #Arrays #CPlusPlus #ProblemSolving #DSA
LeetCode 324 Wiggle Sort II Solution
More Relevant Posts
-
Day 32 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Most Frequent Character in a String We were given a string s. All characters are lowercase. Task was to find the character with maximum frequency. If multiple characters have same frequency, return the lexicographically smaller one. Example: "testsample" → e 💻 Approach 🔹️Create a frequency array of size 26. 🔹️Traverse the string and count each character. 🔹️Find the character with maximum frequency. 🔹️If frequencies are same, pick smaller character. Simple counting logic. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) Fixed array of size 26. 📚 What I learned today: ▫️Frequency arrays are very useful for string problems. ▫️Handling tie conditions (lexicographic order) is important. ▫️Constant space solutions are possible with fixed character sets. ▫️Careful comparison logic matters in final result. Day 32 completed. Improving string handling step by step 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 LeetCode Day-6 | Problem #26 — Remove Duplicates from Sorted Array Today I solved Remove Duplicates from Sorted Array, a classic Two Pointer problem that strengthens array manipulation and in-place algorithm skills. 📌 Problem Statement Given a sorted array nums, remove the duplicates in-place such that each unique element appears only once. Return the number of unique elements k. The relative order of elements should remain the same. 💡 Optimized Approach (Two Pointers) Since the array is sorted, duplicates appear next to each other. ✔ Use two pointers i → points to the last unique element j → scans the array ✔ When nums[j] is different from nums[i] → move it to i + 1 ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) (in-place)
To view or add a comment, sign in
-
-
🚀 March LeetCode Challenge: Day 06/31 | Pattern Detection in the March LeetCode Challenge! I tackled LeetCode 1784: Check if Binary String Has at Most One Segment of Ones. This problem is a great exercise in tracking "state" within a string traversal. 🧩 The Challenge We need to determine if a binary string contains at most one contiguous segment of '1's. For example: "110" -> True (One segment) "1101" -> False (Two separate segments of '1's) 💡 My Logic: Tracking the "Closed" State The key to this problem isn't just counting the ones, but identifying when a segment starts and ends. Counting Segments: I used a count variable to track the current group of ones and a max variable (acting as a flag) to remember if we had already completed a segment. Detecting the Break: If I encounter a '0' after having seen some '1's, I "close" that segment by moving the value to max. The Red Flag: If I ever find a new '1' after a segment has already been closed (max != 0), I immediately know there's more than one segment and return false. 📊 Performance Reflection Time Complexity: O(N) – A single, clean pass through the string. Space Complexity: O(1) – Only using two integer variables to track the state. #LeetCode #CodingChallenge #DynamicProgramming #Cpp #SoftwareEngineering #ProblemSolving #MarchCodingChallenge #VITBhopal #DataStructures #DrGViswanathan #VIT
To view or add a comment, sign in
-
-
Day 55/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 80 – Remove Duplicates from Sorted Array II (Medium) 🧠 Approach: Use a two-pointer technique. Allow at most two occurrences of each number by comparing the current element with the element at index k - 2. If they are different, place the element at position k. 💻 Solution: class Solution: def removeDuplicates(self, nums: List[int]) -> int: if len(nums)<=2: return len(nums) k=2 for i in range(2,len(nums)): if nums[i]!=nums[k-2]: nums[k]=nums[i] k+=1 return k ⏱ Time | Space: O(n) | O(1) 📌 Key Takeaway: The two-pointer technique helps modify arrays in-place while maintaining constraints efficiently. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🧠 Problem: 1758 – Minimum Changes To Make Alternating Binary String Today’s problem was simple on the surface but a good reminder of pattern-based thinking. We’re given a binary string and can flip any character. The goal is to make the string alternating (no two adjacent characters equal) with minimum operations. Instead of overcomplicating it, I focused on one key observation: There are only two possible valid alternating patterns: Starting with '0' → "010101..." Starting with '1' → "101010..." So the approach is straightforward: Assume pattern starts with '0' and count mismatches. Assume pattern starts with '1' and count mismatches. Return the minimum of both counts. 💡 Time Complexity: O(n) 💡 Space Complexity: O(1)
To view or add a comment, sign in
-
-
LeetCode Practice: Today I solved problem no.(169. Majority Element). Problem Statement: We are Given an array of size 'n' and our task is to return the majority element A majority element is a element that appears more than (n/2) times in the array Approach: Hash Map / Frequency Counting Key Idea: Count how many times each element appear in the array and then check which element occurs more than (array size)/2. I used: - Use a Map to Store frequency of each element - Traverse the array and update the frequency count - Check which element has frequency grater than (n/2). Time complexity- O(n log n) Space complexity - O(n) #DSA #ProblemSolving #LeetCode
To view or add a comment, sign in
-
-
🧩 LeetCode Challenge – Day 71 ✅ Today’s problem was a classic string comparison task that tests careful frequency tracking. 🔗 LeetCode 242 – Valid Anagram The idea is simple: two strings are anagrams if they contain the same characters with the same frequencies. Using a frequency array makes the check efficient and straightforward. 💡 Key Takeaways: • Frequency counting is a reliable technique for string comparison • Fixed-size arrays work well when the character set is known • Simple problems reinforce strong fundamentals #Day71 #LeetCodeChallenge #365DaysOfCode #DSA #CodingJourney #ProblemSolving #Strings #Hashing
To view or add a comment, sign in
-
-
🚀 Day 25 of My LeetCode Journey 💻🔥 Today I solved: 189. Rotate Array 🔹 Problem Summary: Given an array, rotate it to the right by k steps. Example: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] 💡 What I Learned: ✅ How to handle array index manipulation ✅ Importance of using k = k % n to avoid unnecessary rotations ✅ Optimizing from brute force ➝ O(n) time complexity ✅ Using the reverse approach for clean and efficient solution
To view or add a comment, sign in
-
Day 2/100 of my LeetCode Challenge🚀 Problem of the Day #868: Binary Gap - Finding gaps in binary representations! Today's problem was deceptively simple but had an elegant bit manipulation solution. The Challenge: Given a positive integer, find the longest distance between any two adjacent 1's in its binary representation. Instead of converting to string and storing all positions in a list (which works but wastes space), we can track only the previous 1's position while scanning bits from right to left. Key Insights: - Bit manipulation (n & 1, n >>= 1) is more efficient than string conversion - We only need to track consecutive 1's, not all pairs - Time complexity: O(log n), Space: O(1) What I Learned: Sometimes the most intuitive solution (convert to string, store positions) isn't the most optimal. Thinking in bits can lead to cleaner, more efficient code! #leetcode #bitmanipulation #100DaysOfCode
To view or add a comment, sign in
-
-
Day 64/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 189 – Rotate Array (Medium) 🧠 Approach: Use the array reversal technique. Reverse the entire array Reverse the first k elements Reverse the remaining elements This rotates the array in-place. 💻 Solution: class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) k = k % n def reverse(l, r): while l < r: nums[l], nums[r] = nums[r], nums[l] l += 1 r -= 1 reverse(0, n - 1) reverse(0, k - 1) reverse(k, n - 1) ⏱ Time | Space: O(n) | O(1) 📌 Key Takeaway: Reversal is a powerful in-place technique for solving array rotation problems efficiently. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
Explore related topics
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