📘 DSA Journey — Day 19 Today’s focus: Sliding Window with uniqueness and distance constraints. Problems solved: • Minimum Consecutive Cards to Pick Up (LeetCode 2260) • Substrings of Size Three with Distinct Characters (LeetCode 1876) Concepts used: • Sliding Window / Two-pointer technique • HashMap / Set for tracking duplicates • Window size and distance calculation Key takeaway: In Minimum Consecutive Cards to Pick Up, the goal is to find the smallest subarray containing duplicate elements. A HashMap is used to store the last index of each card. When a duplicate is found, we calculate the distance between indices and update the minimum length. In Substrings of Size Three with Distinct Characters, a fixed-size sliding window (size = 3) is used. We check whether all characters in the window are distinct using a set or frequency array, and count such valid substrings. Both problems highlight how tracking element positions or uniqueness helps efficiently solve problems involving constraints on subarrays or substrings. Continuing to strengthen sliding window patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
Sliding Window Techniques for DSA Problems
More Relevant Posts
-
📘 DSA Journey — Day 18 Today’s focus: Sliding Window with uniqueness constraint. Problem solved: • Maximum Erasure Value (LeetCode 1695) Concepts used: • Sliding Window / Two-pointer technique • HashMap for tracking elements • Maintaining running sum Key takeaway: The goal is to find the maximum sum of a subarray with all unique elements. Using a sliding window, we expand the window while elements are unique. If a duplicate is encountered, we shrink the window from the left until the duplicate is removed. A HashMap helps track the presence (or last occurrence) of elements efficiently. At the same time, we maintain a running sum of the current window. Whenever the window is valid (all unique), we update the maximum sum. This approach avoids recalculating subarrays and reduces the complexity to O(n). This problem highlights how combining sliding window + HashMap + running sum can efficiently handle uniqueness constraints. Continuing to strengthen pattern recognition and consistency in solving DSA problems. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 24 of my #100DaysOfCode Journey Today, I solved the LeetCode problem Search in 2D Matrix. Problem Insight: Given a sorted 2D matrix, the goal is to efficiently determine whether a target value exists. Approach: Started from the top-right corner of the matrix If the current element equals target → return true If target is smaller → move left (col--) If target is larger → move down (row++) This works because the matrix is sorted row-wise and column-wise, allowing us to eliminate one row or column at each step. Time Complexity: O(m + n) — linear traversal across rows and columns Takeaway: A smart starting point (top-right corner) can significantly optimize search problems in 2D structures. #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #Matrix
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟱𝟭. 𝗖𝗼𝘂𝗻𝘁 𝗡𝗲𝗴𝗮𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿𝘀 𝗶𝗻 𝗮 𝗦𝗼𝗿𝘁𝗲𝗱 𝗠𝗮𝘁𝗿𝗶𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks simple — but the optimal solution is a beautiful staircase traversal that most people miss. 𝙏𝙝𝙚 𝙜𝙧𝙞𝙙 𝙥𝙧𝙤𝙥𝙚𝙧𝙩𝙮: Rows and columns are both sorted in non-increasing order. That means once you hit a negative, everything below it in the same column is also negative. 𝙎𝙩𝙖𝙞𝙧𝙘𝙖𝙨𝙚 𝙖𝙥𝙥𝙧𝙤𝙖𝙘𝙝 (𝙩𝙤𝙥-𝙧𝙞𝙜𝙝𝙩 𝙘𝙤𝙧𝙣𝙚𝙧): ✅ Start at row 0, last column ✅ If grid[row][col] < 0 → all elements below are negative too → add (rows - row) to count, move left ✅ If grid[row][col] >= 0 → move down 𝘾𝙤𝙢𝙥𝙡𝙚𝙭𝙞𝙩𝙮: ⏱ Time: O(m + n) — at most m+n steps 📦 Space: O(1) The naive brute force is O(m×n). This approach uses the sorted structure to skip entire column segments in one shot. That's the difference between knowing a pattern and just looping. This staircase trick works on any sorted matrix — keep it in your toolkit! 🧠 📂 𝑭𝒖𝒍𝒍 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒏 𝑮𝒊𝒕𝑯𝒖𝒃: https://lnkd.in/g_Edk5d3 17 more days. Staying consistent! 💪 #LeetCode #Day83of100 #100DaysOfCode #Java #DSA #Matrix #TwoPointers #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Day 13 – DSA Practice Today I explored the Sliding Window technique using the problem: Maximum Sum Subarray of Size K 📌 Problem: Given an array and a window size k, find the maximum sum of any subarray of size k. 💡 Approach: Instead of recalculating sum every time, maintain a window sum: • Add next element • Remove previous element This helps process the array in a single pass ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem helped me understand how sliding window optimizes subarray problems efficiently. #Java #DSA #SlidingWindow #CodingInterview #ProblemSolving
To view or add a comment, sign in
-
-
🔄 Solved “Spiral Matrix”, a classic matrix traversal problem using boundary-based approach. ✦ What This Problem Taught Me: • How to traverse a 2D matrix layer by layer using boundaries • Effective use of four pointers (top, bottom, left, right) • Managing direction-based traversal (→ ↓ ← ↑) • Importance of shrinking boundaries after each iteration • Avoiding duplicate traversals using proper conditions • Strengthened understanding of 2D arrays and indexing 🚀 Problems like this show how controlling boundaries and direction can simplify complex matrix traversals into a clean O(m × n) solution. On to solving more matrix and pattern-based problems 💪🔥 #LeetCode #DSA #Matrix #TwoPointers #CodingJourney #ProblemSolving #Java #100DaysOfCode
To view or add a comment, sign in
-
-
✨ Day 38 of 90 – Pattern Mastery Journey 🧠 Pattern:Binary Triangle Pattern 💡 Approach: ✔ Used nested loops to control rows and columns ✔ Applied a simple condition `(i + j) % 2` to alternate values ✔ Printed ‘1’ when the sum is even, otherwise ‘0’ ✔ No extra variables needed — clean and efficient logic 🚀 This problem helped me understand how **mathematical conditions can simplify pattern logic**, making the code more optimized and readable. #PatternMasteryJourney #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 26 Problem: 3548. Equal Sum Grid Partition II Topic: Array, Matrix, Prefix Sum, HashSet, Greedy 📌 Quick Problem Sense: You're given an m × n integer grid. Make one straight cut, horizontal or vertical to split it into two non-empty parts with equal sums. The twist? You're allowed to remove at most one border element (right at the cut edge) from either side to balance the sums. Return true if such a partition is possible! 🧠 Approach (Simple Thinking): 🔹 Compute the total sum of the grid upfront 🔹 Scan row by row, maintain a running top sum, derive bottom = total - top 🔹 The imbalance is diff = top - bottom 🔹 A cut is valid if: diff == 0 → already perfectly balanced ✅ diff equals a corner cell of the top section ✅ diff equals the left-border cell of the current bottom row ✅ diff exists in a HashSet of all values seen so far ✅ 🔹 Use a HashSet to track all border values already scanned, O(1) lookup to check if removing one element fixes the imbalance 🔹 For vertical cuts → simply transpose the matrix and reuse the same horizontal cut logic! 🔹 Also try reversed row order to cover cuts scanned from the opposite direction, 4 passes total, all reusing the same function 🔄 ⏱️ Time Complexity: 4 passes through the grid (original, reversed, transposed, transposed+reversed) → O(m × n) Single scan per pass, HashSet lookups in O(1) — clean and efficient! 📦 Space Complexity: HashSet of seen values → O(m × n) worst case Transpose grid → O(m × n) Overall → O(m × n) I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/g2FHaT4S If you solved it by explicitly enumerating only the border cells, or used a different balance-check trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #Programming
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 20 Today’s focus: Sliding Window with frequency constraints. Problem solved: • Maximum Number of Occurrences of a Substring (LeetCode 1297) Concepts used: • Sliding Window technique • Frequency tracking using HashMap / array • Substring counting Key takeaway: The goal is to find the maximum frequency of any substring that satisfies: • At most maxLetters distinct characters • Length between minSize and maxSize A key observation simplifies the problem: We only need to check substrings of size minSize, because larger substrings will have equal or lower frequency. Using a sliding window of fixed size minSize, we: • Track character frequencies inside the window • Ensure the number of distinct characters ≤ maxLetters • Count valid substrings using a HashMap This avoids checking all possible substring sizes and reduces complexity significantly. This problem highlights how constraints can reduce the search space, making sliding window solutions more efficient. Continuing to strengthen pattern recognition and consistency in solving DSA problems. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 - 48/60 Problem: Rotate Array by One (Right Rotation) 🔍 Learned: To rotate the array by one position to the right, store the last element, shift all elements one step to the right, and place the stored element at the beginning. 😅 Struggles: Initially mixed up left and right rotation logic and tried shifting from left to right, which caused overwriting and wrong results. Also made indexing mistakes like accessing invalid positions. 🧠 Key Learning: Always decide direction first (left vs right) before coding. For right shift, traverse from end to start to avoid overwriting. Store the element that will be lost (last element here) before shifting. 📦 Concepts Used: #Arrays #Traversal #InPlace #LogicBuilding #DSA Direction clarity + correct traversal = clean solution. 🚀 #Java #CodingJourney #ProblemSolving #Consistency
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