📌 Day 22/100 – Sort Colors (LeetCode 75) 🔹 Problem: Given an array containing 0s, 1s, and 2s (representing red, white, and blue), sort them in-place so that all same colors stay together in the order: 0 → 1 → 2. No built-in sorting allowed. 🔹 Approach Used: Counting Sort Count occurrences of 0, 1, and 2. Overwrite the array based on those counts. Done in single pass + rewrite, no extra sorting logic needed. 🔹 Time & Space Complexity: ⏳ Time: O(n) — one pass to count, one pass to rewrite 🧠 Space: O(1) — constant extra space 🔹 Key Learnings: ✅ Problems become simpler when we leverage constraints (only 3 values here) ✅ Counting sort is perfect when input range is small & fixed ✅ Always look for in-place optimizations before thinking of extra space #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney
"Sorting 0s, 1s, and 2s using Counting Sort"
More Relevant Posts
-
Day 41 of #100DaysOfCode 💻🔥🚀 Problem: 2090. K Radius Subarray Averages 📊 💡 My Intuition: For each index, we need to calculate the average of the subarray centered at that index with radius k. If the total window size 2*k + 1 exceeds the array length, the average doesn’t exist — so we mark it as -1. To make this efficient, I used a Sliding Window 🪟 — maintaining a running sum instead of recalculating everything again and again. Simple, clean, and fast ⚡ 🧠 Approach: Use two pointers i and j ➡️ to maintain a window of size 2*k + 1. Keep track of the sum using long (to handle big numbers 💪). Once the window size matches, compute the average 🎯 and slide ahead ➡️. ⚙️ Time Complexity: O(n) 💾 Space Complexity: O(n) ✅ Efficient ✅ Handles edge cases (k = 0, large k, etc.) 🚀 Key Takeaway: Even problems that look complicated often crumble when you use the right pattern — Sliding Window keeps things elegant and efficient 💯 #Java ☕ #DSA #LeetCode #ProblemSolving #CodingJourney #100DaysOfCodeChallenge #CodeEveryday
To view or add a comment, sign in
-
-
🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
To view or add a comment, sign in
-
-
💻 Day 11 of #100DaysOfLeetCode – Rotate Image Today’s challenge was “Rotate Image”, a classic matrix manipulation problem that tests both logic and spatial reasoning. 🔹 Problem Statement: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise) — all in place, without using extra memory for another matrix. 🔹 My Approach (in Java): I used a two-step in-place transformation technique: 1️⃣ Transpose the matrix — swap elements across the diagonal (matrix[i][j] ↔ matrix[j][i]). 2️⃣ Reverse each row — to achieve the 90° clockwise rotation. This approach ensures O(1) extra space and O(n²) time complexity, which is optimal for this problem. 🔹 Key Takeaways: ✅ Learned how matrix transformations can be broken down into simpler operations. ✅ Improved understanding of in-place algorithms and memory efficiency. ✅ Reinforced clean coding habits and edge case handling. Every rotation brings me one step closer to mastering problem-solving patterns! 💪 #100DaysOfLeetCode #CodingJourney #RotateImage #Java #DSA #ProblemSolving #LeetCode #CodingChallenge #LearningEveryday
To view or add a comment, sign in
-
-
🔹 Day 62 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: String to Integer (atoi) 🔑 Topic: String Manipulation 🧠 Approach: Step 1: Trim leading whitespaces. Step 2: Check for a sign (+ or -). Step 3: Convert valid digits to integer until a non-digit is found. Step 4: Clamp values beyond 32-bit signed range to [-2³¹, 2³¹ - 1]. Used a long to handle overflow safely before final conversion. ✅ ⏳ Time Complexity: O(n) 💾 Space Complexity: O(1) 📌 Example: Input: "42" → Output: 42 Input: " -042" → Output: -42 🎯 Takeaway: Always handle edge cases — whitespace, signs, and overflow — while converting strings to numbers. 💡 #LeetCode #String #Parsing #Java #ProblemSolving #100DaysOfLeetCodeChallenge 🚀
To view or add a comment, sign in
-
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
To view or add a comment, sign in
-
-
🚀Day 5️⃣9️⃣of #100DaysOfCode Solved LeetCode 3354 – Make Array Elements Equal to Zero 🧮 ⚡ Runtime: 1 ms (Beats 83.13%) 📊 Memory: 42.10 MB (Beats 69.28%) 🔍 Concept: This problem revolves around array traversal, directional logic, and state transitions. You start from an index where the element is 0 and move left or right while updating values and reversing direction — until all elements reach zero. 🧩 Approach Summary: Identify the initial index containing 0. Traverse left and right, updating values and counting valid selections. Keep it simple, linear, and effective. Some problems test patience as much as skill — but every solved logic strengthens the problem-solving mindset. #LeetCode #Java #ProblemSolving #100DaysOfCode #CodingJourney #SoftwareEngineer #Developer #AlgorithmDesign #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 50 of My LeetCode Journey Today, I solved Reshape the Matrix. Problem in short: Given a matrix, reshape it to a new size r x c while keeping the row-traversal order intact. If it’s impossible, return the original matrix. Approach I used: First, check if total elements match (originalRows * originalCols == r * c). Use a single counter to traverse the original matrix and place elements in the new matrix. Map the counter to new matrix indices: row = count / c col = count % c Why this is clean: Simple and intuitive. No need for multiple variables for row and column. Works for any size matrix. Example: Original: 1 2 3 4 Reshaped to 1×4 → 1 2 3 4 This problem taught me how matrix manipulation can be simplified using smart indexing. #100DaysOfLeetCode #CodingJourney #ProblemSolving #DSA #Java #Matrix
To view or add a comment, sign in
-
-
🔹 Day 46: Is Subsequence (LeetCode #392) 📌 Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some (possibly none) characters from the original string without disturbing the relative order of the remaining characters. ✅ My Approach: I used a two-pointer technique — one pointer iterates through string t, and the other tracks progress through string s. Each time a matching character is found, the pointer for s moves forward. If we reach the end of s, it means all its characters appeared in sequence within t. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 68.53%) Memory: 41.32 MB (Beats 87.28%) 💡 Reflection: A simple yet elegant problem that highlights how pointer movement can efficiently handle string comparisons without extra memory usage. ✨ #LeetCode #Java #Strings #TwoPointers #100DaysOfCode #Day46
To view or add a comment, sign in
-
-
🔥 **Blind 75 Challenge – Day 8: Longest Consecutive Sequence (LeetCode #128)** Today’s problem focuses on **detecting consecutive sequences efficiently** using a HashSet — one of the most elegant O(n) solutions in the #Blind75 list. 🧩 **Problem Statement:** Given an unsorted array of integers, return the length of the longest consecutive elements sequence. You must solve it in **O(n)** time. **Example:** Input: `[100, 4, 200, 1, 3, 2]` Output: `4` (sequence: 1, 2, 3, 4) 💭 **My Approach:** * Store all numbers in a **HashSet** for O(1) lookups * For each number, start counting only if it’s the **beginning of a sequence** (`num - 1` not in set) * Expand forward until the sequence breaks * Track the longest streak 🧠 **Time Complexity:** O(n) 💾 **Space Complexity:** O(n) 💡 **Key Takeaway:** This problem reinforces how **sets and linear scans** can replace nested loops — a simple yet powerful optimization mindset. #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #DailyCoding #CareerGrowth #Blind75
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
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