✳️Day 51 of #100DaysOfCode✳️ 🚀 Cracking the Sliding Window: LeetCode 1004 (Max Consecutive Ones III) I recently tackled this popular interview question using an efficient Sliding Window approach in Java. 🔹The challenge: Find the maximum number of consecutive 1s in a binary array if you can flip at most k 0s. ⭐Here is the step-by-step breakdown of how my solution works: 🔸Initialize Two Pointers: Use a fast pointer (j) to expand the window and a slow pointer (i) to contract it, keeping track of the maxLen and the number of zeroes encountered. 🔸Expand the Window: Iterate through the array with j. If nums[j] == 0, increment the zero counter. 🔹Shrink When Invalid: If the number of zeros exceeds k, move the left pointer i forward. If the element leaving the window (nums[i]) is a 0, decrement the zero counter. 🔹Update Max Length: At each step, calculate the valid window size using j - i + 1 and update the maximum length found so far. 🔹Complexity: Time Complexity: O(N) – Each element is visited at most twice. Space Complexity: O(1) – Optimal memory usage with no extra data structures. Always satisfying to see all test cases pass with clean, readable code! 💻✨ #LeetCode #Java #DataStructures #Algorithms #SlidingWindow #CodingLife #SoftwareEngineering
Cracking LeetCode 1004 Max Consecutive Ones III with Sliding Window in Java
More Relevant Posts
-
Today I tackled the Sequence Equation problem, which involves working with permutations and index mapping. 🔍 Problem Insight: Given a permutation p, find values of y such that: 👉 p[p[y]] = x for all x from 1 to n 💡 Key Idea: Instead of brute force, I used: Position mapping for fast lookup Double indexing trick (pos[pos[x]]) 🚀 Why this works: It reduces complexity from O(n²) to O(n), making it highly efficient. 🧠 What I learned: Importance of inverse mapping Optimizing lookup operations Writing clean and efficient Java code 📌 Example: Input: [5, 2, 1, 3, 4] Output: [4, 2, 5, 1, 3] 🔖 Tags #Java #DataStructures #Algorithms #CodingChallenge #ProblemSolving #100DaysOfCode #DeveloperJourney #SoftwareDeveloper #Hackerrank #CodingPractice #TechLearning
To view or add a comment, sign in
-
-
🚀 LeetCode 345 – Reverse Vowels of a String | Two Pointer Technique Today I solved a simple yet interesting string manipulation problem using the Two Pointer approach. 🔹 Problem Summary: Reverse only the vowels in a string while keeping all other characters in their original positions. Example: hello → holle 🔹 Approach Used: One pointer starts from the beginning Another pointer starts from the end Move both pointers until vowels are found Swap the vowels and continue 🔹 What I Learned: Efficient use of the Two Pointer Technique Better understanding of string traversal Handling character conditions cleanly in Java 🔹 Why this problem matters: Although the problem looks easy, it strengthens important concepts frequently used in interviews: String manipulation Pointer movement logic Optimization from brute force approaches 💡 Small problems like this help improve coding clarity and build strong fundamentals for advanced algorithms. #LeetCode #DSA #Java #TwoPointers #ProblemSolving #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 58 of #LeetCode Challenge 🔍 Problem: Decode the Slanted Ciphertext Today’s problem was all about understanding patterns and matrix traversal in a clever way! 💡 Key Idea: * The encoded string is formed by writing characters in a matrix row-wise * The trick is to read diagonally (↘️ direction) to decode the original message * No need to build a 2D matrix — we can directly calculate indices! 🧠 What I Learned: * How to convert 1D string into virtual 2D matrix * Diagonal traversal techniques * Importance of trimming trailing spaces in string problems ⚡️ Approach: * Calculate number of columns → cols = n / rows * Traverse diagonally from each column * Append characters using index formula i * cols + j * Remove extra spaces at the end 💻 Language: Java 🎯 Time Complexity: O(n) 📦 Space Complexity: O(n) Consistency is the key 🔑 — one problem at a time, getting better every day! #Day31 #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Solved of all 4 problems from LeetCode Weekly Contest #501 (Easy → Hard) using Java. This contest tested my problem-solving skills across arrays, string parsing, number theory, graph algorithms, and optimization. Here’s a quick breakdown of my approach: 🔹 Q1. Concatenate Array With Reverse Approach: Used a single traversal to construct the result array efficiently. The first half stores the original elements, while the second half stores the same elements in reverse order. Code: https://lnkd.in/gfzAKbVq 🔹 Q2. Count Valid Word Occurrences Approach: Used String Parsing + HashMap. Parsed the concatenated string character by character, extracted valid words based on problem constraints, and stored frequencies for fast query lookup. Code: https://lnkd.in/g-tP4CR8 🔹 Q3. Minimize Array Sum Using Divisible Replacements Approach: Used factor enumeration + presence mapping. For each number, checked all divisors up to √n, identified the smallest valid factor present in the original array, and minimized the final sum. Code: https://lnkd.in/gxYBczeR 🔹 Q4. Minimum Cost to Buy Apples II Approach: Used Graph + Dijkstra’s Algorithm. For every city, computed the minimum travel cost and tax cost separately, then evaluated the optimal city to buy apples with minimum total cost. Code: https://lnkd.in/gv86XEKz 💡 Key takeaway: Consistency under pressure improves both coding speed and decision-making. Would love to know—how did your contest go? #leetcode #dsa #java #softwareengineering #competitiveprogramming #problemsolving #learninginpublic #coding
To view or add a comment, sign in
-
-
💡 Day 55 – Solved Problem 240: "Search a 2D Matrix II" using Java! Today I worked on an optimized matrix search problem 🔥 🔍 Problem: Search for a target value in a matrix where rows are sorted left-to-right and columns are sorted top-to-bottom. 🧠 Key Insight: Starting from the top-right corner allows us to eliminate an entire row or column at every step. 🚀 Approach: If current value > target → move left If current value < target → move down Continue until target is found or search space ends ⏱ Time Complexity: O(m + n) 📦 Space Complexity: O(1) This problem helped me understand how matrix properties can drastically optimize searching techniques! #LeetCode #Problem240 #Java #Matrix #Search #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟲/𝟳𝟱 𝗗𝗮𝘆𝘀 𝗼𝗳 #𝗖𝗼𝗱𝗲𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Today's problem: 𝗜𝘀 𝗦𝘂𝗯𝘀𝗲𝗾𝘂𝗲𝗻𝗰𝗲 (𝗟𝗲𝗲𝘁𝗰𝗼𝗱𝗲 𝟯𝟵𝟮) This problem helped me improve my understanding of the 𝘁𝘄𝗼-𝗽𝗼𝗶𝗻𝘁𝗲𝗿 𝘁𝗲𝗰𝗵𝗻𝗶𝗾𝘂𝗲, string traversal, and sequence matching logic. 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁: A string is considered a subsequence if all its characters appear in another string in the same relative order without changing the sequence. 𝗪𝗵𝗮𝘁 𝗜 𝗟𝗲𝗮𝗿𝗻𝗲𝗱: • Applying the 𝘁𝘄𝗼-𝗽𝗼𝗶𝗻𝘁𝗲𝗿 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 for sequence comparison • Traversing two strings simultaneously in an optimized way • Understanding 𝗿𝗲𝗹𝗮𝘁𝗶𝘃𝗲 𝗼𝗿𝗱𝗲𝗿 matching in strings • Improving logical thinking for 𝘀𝘁𝗿𝗶𝗻𝗴-𝗯𝗮𝘀𝗲𝗱 problems • Writing clean and efficient conditional traversal logic 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: • Initialize two pointers for both strings • Compare characters at the current positions • Move both pointers when characters match • Move only the second pointer when characters do not match • Check whether all characters of the first string are completely traversed 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀: 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n) → Single traversal through the strings 𝗦𝗽𝗮𝗰𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(1) → Constant extra space used Successfully solved and understood how the two-pointer technique simplifies subsequence matching problems efficiently. Daily consistency is helping me strengthen my 𝗗𝗦𝗔 foundations and problem-solving mindset step by step. 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲 𝗨𝘀𝗲𝗱: Java 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 𝗔𝗰𝗵𝗶𝗲𝘃𝗲𝗱: 2 ms #Day6 #75DaysOfCode #Java #DSA #LeetCode #CodingJourney #ProblemSolving #Programming #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
StackOverflowError and OutOfMemoryError aren't random. They're telling you exactly which part of JVM memory you broke. Here's the map that makes both make sense 👇 Most developers write Java for years without knowing where their variables actually live. But understanding JVM memory is what separates "code that runs" from "code you can debug and optimize." Here's the mental model: 🔹 Per JVM (shared across all threads) → Heap Area & Method Area. One JVM, one Heap, one Method Area. 🔹 Per Thread (private to each thread) → Stack, PC Register & Native Method Stack. Every thread gets its own. And where do your variables go? → Static variables → Method Area → Instance variables → Heap → Local variables → Stack This single concept quietly explains so much — why local variables are thread-safe, why instance variables aren't, and why a StackOverflowError (too-deep recursion blowing the thread's Stack) is a completely different problem from an OutOfMemoryError (Heap exhausted). Understand memory, write better code. Save this for your next interview prep 📌 What's one Java concept you wish someone had explained visually when you were starting out? #Java #JVM #SoftwareEngineering #ProgrammingFundamentals #LearnInPublic #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 85 of consistency — and a major milestone unlocked: 120 LeetCode problems solved this year! 🔥 Today, I focused on Longest Palindromic Substring (LeetCode 5) using the Expand Around Center approach in Java. Instead of brute-forcing every substring with (O(n^3)) complexity, the key insight was recognizing a palindrome’s symmetry. 💡 Core Idea Every palindrome expands from a center: • Odd length → center is a character (aba) • Even length → center is between characters (abba) By expanding outward while characters match, I reduced the solution to: ✅ Time Complexity: (O(n^2)) ✅ Space Complexity: (O(1)) 📊 Today's Stats • Runtime: 18ms • Memory: 43.42 MB • Current Streak: 85 Days 🔥 • Total Solved: 120 Problems This problem reinforced an important lesson: Strong problem-solving comes from identifying patterns, not memorizing solutions. Next goal: keep sharpening pattern recognition for interviews and scalable thinking. What’s your favorite string algorithm or interview pattern to solve? 👇 #DSA #Java #LeetCode #SoftwareEngineering #CodingInterview #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
Nested encoded strings can look complex at first, but stacks make the decoding process systematic and manageable. 🚀 Day 144/365 — DSA Challenge Solved: Decode String Problem idea: Given an encoded string in the format: k[encoded_string] Repeat the string inside brackets exactly k times and return the fully decoded result. Efficient approach: 1. Traverse the string character by character 2. Build numbers for repeat counts 3. Use stacks to store: - Previous strings - Repeat counts 4. When ] appears: - Repeat current substring - Merge it with previous stored string Key Learning: - Stack-based string decoding - Handling nested patterns - Managing multiple states simultaneously - Parsing strings efficiently ⏱ Time: O(n) 📦 Space: O(n) Day 144/365 complete. 💻 221 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #Stack #String #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭/𝟳𝟱 𝗗𝗮𝘆𝘀 𝗼𝗳 #𝗖𝗼𝗱𝗲𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Today's problem: 𝗠𝗲𝗿𝗴𝗲 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝗔𝗹𝘁𝗲𝗿𝗻𝗮𝘁𝗲𝗹𝘆 (𝗟𝗲𝗲𝘁𝗰𝗼𝗱𝗲 𝟭𝟳𝟲𝟴) This problem helped me understand the basics of 𝗌𝗍𝗋𝗂𝗇𝗀 𝗍𝗋𝖺𝗏𝖾𝗋𝗌𝖺𝗅, 𝗮𝗹𝘁𝗲𝗿𝗻𝗮𝘁𝗶𝗻𝗴 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿 𝗶𝗻𝘀𝗲𝗿𝘁𝗶𝗼𝗻, and handling different string lengths efficiently. 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁: Merge two strings by taking characters alternately from each string while preserving their original order. 𝗪𝗵𝗮𝘁 𝗜 𝗟𝗲𝗮𝗿𝗻𝗲𝗱: • Efficient usage of 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 for string manipulation. • Managing 𝘁𝘄𝗼 𝗽𝗼𝗶𝗻𝘁𝗲𝗿𝘀 𝘀𝗶𝗺𝘂𝗹𝘁𝗮𝗻𝗲𝗼𝘂𝘀𝗹𝘆 during traversal. • Handling 𝗲𝗱𝗴𝗲 𝗰𝗮𝘀𝗲𝘀 when strings have unequal lengths. • Writing 𝗰𝗹𝗲𝗮𝗻 𝗮𝗻𝗱 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗹𝗼𝗼𝗽𝗶𝗻𝗴 𝗹𝗼𝗴𝗶𝗰. • Improving understanding of 𝗯𝗮𝘀𝗶𝗰 𝘀𝘁𝗿𝗶𝗻𝗴-𝗯𝗮𝘀𝗲𝗱 𝗽𝗿𝗼𝗯𝗹𝗲𝗺 𝘀𝗼𝗹𝘃𝗶𝗻𝗴. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: • Initialize 𝘁𝘄𝗼 𝗽𝗼𝗶𝗻𝘁𝗲𝗿𝘀 for both strings. • Traverse both strings using a loop. • Append characters alternately into the result string. • Continue until both strings are completely traversed. • Append remaining characters from the longer string if any are left. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀: 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n) → Each character is processed once. 𝗦𝗽𝗮𝗰𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n) → Space used for storing the merged string. Successfully solved and understood the logic behind alternating string traversal. Excited to continue this 𝟳𝟱 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗖𝗼𝗱𝗲 journey and improve my 𝗽𝗿𝗼𝗯𝗹𝗲𝗺-𝘀𝗼𝗹𝘃𝗶𝗻𝗴 𝘀𝗸𝗶𝗹𝗹𝘀 consistently. 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲 𝗨𝘀𝗲𝗱: Java 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 𝗔𝗰𝗵𝗶𝗲𝘃𝗲𝗱: 1 ms #Day1 #75DaysOfCode #Java #DSA #LeetCode #CodingJourney #ProblemSolving #Programming #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-