Sliding Window Maximum with Deque in O(n) Time

🔥 Day 74/100 of #100DaysOfCode – Sliding Window Maximum: Deque for O(n)! Today solved a classic problem requiring efficient maximum tracking in a moving window: ✅ Problem 239: Sliding Window Maximum Task: Return the maximum in each sliding window of size k in O(n) time. Approach: Monotonic decreasing deque (Double-ended Queue): Store indices (not values) in deque, maintaining decreasing order of values Remove out-of-window indices from front Remove smaller elements from back before adding new index Front of deque always holds current window’s max Key Insight: A deque lets us efficiently maintain candidates for maximum while sliding the window, avoiding repeated O(k) scans. Complexity: O(n) time, O(k) extra space — optimal linear solution. A powerful example of using the right data structure to maintain order while supporting efficient insertions and deletions from both ends! 📊🔍 #100DaysOfCode #LeetCode #Java #Deque #SlidingWindow #MonotonicQueue #Algorithms #CodingInterview

  • graphical user interface, text

Another One , bro is playing on hard mode with ease 💪🏼

To view or add a comment, sign in

Explore content categories