🚀 Day 45 of #100DaysOfCode Solved 209. Minimum Size Subarray Sum on LeetCode 📊 🧠 Key Insight: We need to find the smallest length subarray whose sum is ≥ target. A brute-force approach would be O(n²), but this can be optimized using the Sliding Window (Two Pointers) technique. ⚙️ Approach: 1️⃣ Initialize two pointers: left = 0 and iterate right 2️⃣ Keep adding elements to curr_sum 3️⃣ Once curr_sum ≥ target, try to shrink the window: 🔹Update minimum length 🔹Remove nums[left] and move left forward 4️⃣ Repeat until the entire array is processed This ensures we always maintain the smallest valid window. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #SlidingWindow #Arrays #Algorithms #Java #InterviewPrep #CodingJourney
Optimizing LeetCode's Minimum Size Subarray Sum with Sliding Window Technique
More Relevant Posts
-
Day 56/100 | #100DaysOfDSA 🧠⚡ Today’s problem: String Compression A clean in-place array manipulation problem. Core idea: Compress consecutive repeating characters and store the result in the same array. Approach: • Traverse the array using a pointer • Count consecutive occurrences of each character • Write the character to the array • If count > 1 → write its digits one by one • Move forward and repeat Key insight: We don’t need extra space — just carefully manage read & write pointers. Time Complexity: O(n) Space Complexity: O(1) Big takeaway: In-place algorithms require precise pointer control but give optimal space efficiency. Mastering these improves real-world memory optimization skills. 🔥 Day 56 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Strings #TwoPointers #InPlace #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode Solved 719. Find K-th Smallest Pair Distance on LeetCode 📏 🧠 Key Insight: We need to find the k-th smallest absolute difference among all possible pairs in the array. Brute force (generating all pairs) would be O(n²) → not efficient. Instead, we use Binary Search on Answer + Two Pointers. ⚙️ Approach: 1️⃣ Sort the array 2️⃣ Define search space: 🔹left = 0 (minimum distance) 🔹right = max(nums) - min(nums) 3️⃣ Apply Binary Search: 🔹For a given mid, count how many pairs have distance ≤ mid 4️⃣ Counting pairs efficiently: 🔹Use two pointers 🔹For each i, move j while nums[j] - nums[i] ≤ mid 🔹Add (j - i - 1) to count 5️⃣ If count ≥ k → try smaller distance 6️⃣ Else → increase distance 🎯 Final answer = smallest distance satisfying the condition ⏱️ Time Complexity: O(n log n + n log D) 🔹Sorting + Binary Search with linear check 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #TwoPointers #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🧠 Day 40 / 100 – DSA Practice Solved String to Integer (atoi) on LeetCode 🔢➡️🧮✅ 🔹 Problem: Convert a string into a 32-bit signed integer while handling whitespace, signs, non-digit characters, and overflow conditions. 🔹 Approach: Followed a step-by-step simulation: Ignore leading whitespaces Handle optional + / - sign Convert digits while checking for overflow Stop at first non-digit character 🔍 Key Insight: Careful handling of edge cases like overflow and invalid input is crucial in this problem 🔹 Complexity: ⏱ Time → O(n) 📦 Space → O(1) 💯 Result: ✔️ All test cases passed ⚡ Runtime: 1 ms (Beats 100%) One of the best problems to master string parsing & edge case handling 🚀 #Day40 #100DaysOfCode #LeetCode #Java #DSA #Strings #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
Day 103: Simple & Clean 🎯 Problem 1848: Minimum Distance to the Target Element After some complex DP challenges, today was a straightforward exercise in linear search and distance calculation. The Strategy: • Linear Traversal: I iterated through the array to find every occurrence of the target element. • Absolute Minimization: For each match, I calculated the absolute difference between the current index and the start index, keeping track of the minimum value found. Sometimes a simple, O(N) solution is all you need. Day 103 down—maintaining the streak with clarity and consistency. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
Day 33/75 — Sort an Array (Merge Sort) Today’s problem required sorting an array in O(n log n) time without using built-in sort. Approach: • Divide array into two halves • Recursively sort both halves • Merge sorted halves Key idea: return merge(left, right); Merge step ensures elements are placed in sorted order. Time Complexity: O(n log n) Space Complexity: O(n) This problem reinforced the concept of Divide & Conquer. 33/75 🚀 #Day33 #DSA #MergeSort #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 51/100 | #100DaysOfDSA 🚀⚡ Today’s problem: Product of Array Except Self Given an array, return a new array where each element is the product of all elements except itself — without using division. Brute force? Too slow. Division? Not allowed. Optimized Approach: • Build prefix product array (left to right) • Then multiply with suffix product (right to left) • No extra space needed (excluding output) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Breaking a problem into prefix + suffix patterns can unlock optimal solutions. Clean logic. Efficient approach. 💯 Day 51 done. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #Arrays #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 97/200 – LeetCode Challenge Solved “Largest Rectangle in Histogram” (Hard) today. This problem is a great example of how powerful the Monotonic Stack technique can be. Instead of brute force, we efficiently determine how far each bar can extend to compute the maximum rectangle area. Using a monotonic increasing stack to track indices. Identifying left and right boundaries for each bar. Every day is making data structures feel more intuitive! #Day96 #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 38 of Daily DSA 🚀 Solved LeetCode 1897: Redistribute Characters to Make All Strings Equal ✅ Problem: Given an array of strings, determine if you can make every string equal by moving any character from one string to any position in another string. Rules: * You can pick two distinct indices i and j and move any character from words[i] to any position in words[j] * Any number of operations are allowed * Return true if all strings can be made equal, false otherwise Approach: Used a HashMap to count the total frequency of every character across all strings. If every character's count is divisible by the number of strings, equal redistribution is possible. Steps: 1. Concatenate all strings into one using StringBuilder 2. Count the frequency of each character using a HashMap 3. For every character count, check if it is divisible by the number of words 4. If any count is not divisible → return false 5. Otherwise → return true ⏱ Complexity: • Time: O(n * m) — n = number of words, m = average word length • Space: O(1) — at most 26 characters in the map 📊 LeetCode Stats: • Runtime: 12 ms (Beats 17.93%) • Memory: 46.63 MB A brilliant insight — redistribution is just a divisibility check! No complex simulation needed. #DSA #LeetCode #Java #HashMap #Strings #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 36/75 — Length of Last Word Today’s problem was about finding the length of the last word in a string. Approach: • Traverse from the end • Skip trailing spaces • Count characters until next space Key logic: while (i >= 0 && s.charAt(i) == ' ') i--; while (i >= 0 && s.charAt(i) != ' ') len++; Time Complexity: O(n) Space Complexity: O(1) Simple problem, but reinforces string traversal techniques. 36/75 🚀 #Day36 #DSA #Strings #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 58/75 — Contiguous Array Today’s problem was about finding the maximum length of a subarray with equal number of 0s and 1s. Approach: • Convert 0 → -1 to transform the problem into finding subarray with sum = 0 • Use HashMap to store first occurrence of prefix sum • If same sum appears again → subarray between them has equal 0s and 1s Key logic: if (nums[i] == 0) sum -= 1; else sum += 1; if (map.containsKey(sum)) { maxLen = Math.max(maxLen, i - map.get(sum)); } else { map.put(sum, i); } Time Complexity: O(n) Space Complexity: O(n) A clever problem that builds strong intuition for prefix sum + hashing. 58/75 🚀 #Day58 #DSA #PrefixSum #HashMap #Java #Algorithms #LeetCode
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