🔥 𝗗𝗮𝘆 𝟴𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟮𝟳𝟲𝟮. 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗼𝘂𝘀 𝗦𝘂𝗯𝗮𝗿𝗿𝗮𝘆𝘀 | 🟡 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 This one is a proper sliding window + monotonic deque problem — one of the most powerful combos in DSA. 𝗧𝗵𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻: A subarray is valid if max - min ≤ 2 for all pairs. Checking this naively for every subarray is O(n²). We need smarter. 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝘄𝗶𝗻𝗱𝗼𝘄 + 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀: ✅ maxDeque → monotonic decreasing (tracks window max) ✅ minDeque → monotonic increasing (tracks window min) ✅ Expand right every iteration ✅ Shrink left when max - min > 2 ✅ Every valid window ending at right contributes (right - left + 1) subarrays 𝗪𝗵𝘆 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀? They give O(1) access to the window's current max and min at all times — no rescanning needed when the window shrinks. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — each element enters and exits each deque at most once 📦 Space: O(n) deque space The moment this pattern clicked for me — sliding window controlling the range, deques tracking the extremes — a whole class of hard problems became approachable. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/e_Mu8-Eh 16 more days. Grinding through! 💪 #LeetCode #Day84of100 #100DaysOfCode #Java #DSA #SlidingWindow #MonotonicDeque #CodingChallenge #Programming
More Relevant Posts
-
Most substring problems can be solved efficiently using the sliding window technique. 🚀 Day 97/365 — DSA Challenge Solved: Longest Substring Without Repeating Characters Problem idea: We need to find the length of the longest substring that contains no duplicate characters. Efficient approach: Use a sliding window with two pointers. Steps: 1. Keep track of the last seen index of each character 2. Expand the window by moving the right pointer 3. If a character repeats inside the window, move the left pointer to the position after its last occurrence 4. Update the maximum window length This ensures the window always contains unique characters. ⏱ Time: O(n) 📦 Space: O(1) Day 97/365 complete. 💻 268 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the smallest subarray with sum ≥ target. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Minimum Size Subarray Sum 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐒𝐥𝐢𝐝𝐢𝐧𝐠 𝐖𝐢𝐧𝐝𝐨𝐰 • Maintained a window using two pointers (left & right) • Expanded the window by moving right pointer • Once sum ≥ target: • Shrunk the window from the left • Updated the minimum length This ensures we always keep the smallest valid window. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Sliding window works well for subarray problems • Expand → to meet condition • Shrink → to optimize result • Two pointers reduce time complexity significantly 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sliding window is about balance — expand when needed, shrink when possible. 67 days consistent 🚀 On to Day 68. #DSA #Arrays #SlidingWindow #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
LeetCode — Problem 5 | Day 12 💡 Problem: Longest Palindromic Substring --- 🧠 Problem: Given a string "s", find the longest substring that is a palindrome. --- 🧠 Approach (Expand Around Center): - Treat each character as a center - Handle two cases: • Odd length → (i, i) • Even length → (i, i+1) - Expand outward while characters match - Track the maximum length substring 👉 Key idea: expand from center instead of checking all substrings --- ⏱ Time Complexity: O(n²) 📦 Space Complexity: O(1) --- 🔍 Insight: A palindrome expands symmetrically from its center --- ⚖️ Edge Cases: - Empty string → return "" - Single character → same character - Even length palindrome (e.g., "bb") - Entire string is a palindrome --- 🔑 Key Learning: - Avoid brute force O(n³) - Efficient pattern: Expand Around Center #LeetCode #DSA #Java #Palindrome #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 25 Today’s focus: Sliding Window with constraint tracking. Problem solved: • Find the Longest Semi-Repetitive Substring (LeetCode 2730) Concepts used: • Sliding Window / Two-pointer technique • Constraint tracking within window • Dynamic window adjustment Key takeaway: The goal is to find the longest substring where there is at most one pair of equal adjacent characters. Using a sliding window, we expand the window while keeping track of how many adjacent equal pairs exist. If the number of such pairs exceeds 1, we shrink the window from the left until the condition is satisfied again. At each step, we maintain the maximum valid window length. This problem shows how sliding window can be adapted not just for counts or sums, but also for tracking specific patterns or conditions inside a substring. Continuing to strengthen pattern recognition and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Keeping the Two-Pointer momentum going! Today's challenge: Valid Palindrome. After using two pointers to reverse arrays and remove duplicates, I applied the exact same pattern to strings. The goal? Check if a string reads the same forwards and backwards (like "racecar"). While you could solve this by creating a reversed copy of the string and comparing the two, that requires O(n) extra memory. The Two-Pointer approach handles it entirely in-place with O(1) space! Here is the logic: • Left Pointer: Starts at the very beginning of the string. • Right Pointer: Starts at the very end. • Compare: If the characters match, great! If they don't, it's not a palindrome. • Move Inward: Step both pointers toward the middle until they cross. It is incredibly satisfying to see how one core algorithmic pattern can be adapted to solve so many different types of problems so efficiently. #DSA #Java #LeetCode #isValidPalindrome
To view or add a comment, sign in
-
-
The Two-Pointer streak continues! Today’s LeetCode Problem: Is Subsequence. After using multiple pointers to sort arrays and move zeroes, I applied the exact same pattern to string manipulation today. The challenge was figuring out if a shorter string (s) is a valid subsequence of a longer string (t) without disturbing the relative order of the characters. Instead of generating all possible subsequences (which would be a massive O(2ⁿ) performance drain), the Two-Pointer approach solves this beautifully in a single pass. Knocking this out in O(n) time and O(1) space is another great reminder of how incredibly versatile this algorithmic pattern is for both arrays and strings. #DSA #Java #LeetCode #IsSebsequenceProblem
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 93/100 ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐧𝐭𝐢𝐧𝐮𝐨𝐮𝐬 𝐈𝐧𝐜𝐫𝐞𝐚𝐬𝐢𝐧𝐠 𝐒𝐮𝐛𝐬𝐞𝐪𝐮𝐞𝐧𝐜𝐞 Today’s problem focused on finding the length of the longest continuous increasing subarray. Unlike LIS, this must be strictly increasing AND contiguous. 💡 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Use a simple linear scan Maintain two variables: curr and maxLen Reset streak when order breaks Time Complexity: O(n), Space: O(1) 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Traverse the array and compare each element with the previous one: If increasing → extend streak Else → reset streak Keep updating maximum length #Day93 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #ProblemSolving #KeepLearning
To view or add a comment, sign in
-
-
🚀 DSA Journey: 15/75 Completed 📌 Problem: Maximum Number of Vowels in a Substring of Given Length Today’s problem was a great example of the Sliding Window technique. 🔍 Key Idea: Instead of checking every substring (which is inefficient), we maintain a window of size k and: Add the next character to the window Remove the previous character Track the maximum number of vowels 💡 This reduces time complexity to O(n) — much better than brute force! 🧠 What I Learned: How to efficiently manage a fixed-size window Importance of updating values while sliding Avoiding unnecessary recalculations 🔥 Progress: 15 / 75 problems done — consistency is the real key! #DSA #CodingJourney #Java #LeetCode #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 33/50 🚀 — Valid Palindrome (Two Pointer Approach) Today’s problem was a great mix of string manipulation + two pointers. 🔹 Ignored non-alphanumeric characters 🔹 Handled case-insensitivity 🔹 Compared characters from both ends efficiently Key insight: Instead of preprocessing the string, we can optimize in-place using two pointers, skipping unwanted characters on the go. 💡 This improves both readability and performance. Performance: ⚡ Runtime: 2 ms (99%+) 📦 Memory: Efficient #Day33 #LeetCode #TwoPointers #DSA #Java #CodingJourney #50DaysOfCode #ProblemSolving
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
-
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