🔥 𝗗𝗮𝘆 𝟴𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟮𝟳𝟲𝟮. 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗼𝘂𝘀 𝗦𝘂𝗯𝗮𝗿𝗿𝗮𝘆𝘀 | 🟡 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 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

To view or add a comment, sign in

Explore content categories