What if you could find the hottest day in every 3-day period without rechecking all days again and again?
Why Sliding Window Maximum Using Deque in DSA Rust?
Imagine you have a long list of daily temperatures, and you want to find the hottest day in every 3-day period. Doing this by checking each group of 3 days one by one feels like repeating the same work over and over.
Manually checking each group means looking at many overlapping days multiple times. This wastes time and can cause mistakes, especially if the list is very long. It's like re-reading the same pages in a book again and again to find the highest number.
Using a sliding window with a deque helps keep track of the hottest days efficiently. The deque stores only the important days, so you don't have to check all days every time. It slides through the list smoothly, updating the hottest day quickly.
for i in 0..(nums.len() - k + 1) { let mut max_val = nums[i]; for j in i..(i + k) { if nums[j] > max_val { max_val = nums[j]; } } println!("{}", max_val); }
use std::collections::VecDeque; let mut deque = VecDeque::new(); for i in 0..nums.len() { while let Some(&back) = deque.back() { if nums[back] <= nums[i] { deque.pop_back(); } else { break; } } deque.push_back(i); if i >= k - 1 { while deque.front().unwrap() < &(i + 1 - k) { deque.pop_front(); } println!("{}", nums[*deque.front().unwrap()]); } }
This method lets you find the maximum in every window quickly, even for very large lists, saving time and effort.
Stock traders use this to find the highest stock price in the last few days to decide when to buy or sell without checking every single day repeatedly.
Manual checking repeats work and is slow for large data.
Deque keeps track of important elements efficiently.
Sliding window maximum finds max values quickly in moving windows.
Practice
Solution
Step 1: First window
Window [1, 3, -1] contains elements at indices 0, 1, 2.Step 2: Maximum
max(1, 3, -1) = 3.Step 3: Deque state
After processing first window: deque = [1] (index of 3, since 3 is largest and -1 is removed because 3 > -1 and 3 came before -1).Final Answer:
3 -> Option CQuick Check:
max(1,3,-1)=3. [OK]
- Returning the last element -1
- Returning the first element 1
- Not recognizing 3 as the maximum
Solution
Step 1: Deque content
The deque stores INDICES (not values), arranged so that nums[deque.front()] is always the maximum of the current window.Step 2: Decreasing order
Elements are kept in decreasing order of their values - smaller elements behind a larger one are useless (they can never be the maximum while the larger element is in the window).Final Answer:
Indices of elements in decreasing value order. -> Option BQuick Check:
Deque = indices in decreasing value order. Front = current window max. [OK]
- Thinking deque stores values not indices
- Thinking all window elements are stored
- Confusing with a max-heap approach
Solution
Step 1: Each element pushed once
Each element is pushed to the deque exactly once.Step 2: Each element popped at most once
Each element is popped from either front or back at most once. Total push+pop operations: 2n = O(n).Step 3: Overall
n iterations Ć O(1) amortized per iteration = O(n).Final Answer:
O(n) - each element pushed and popped at most once. -> Option CQuick Check:
Each element: 1 push + at most 1 pop = O(n) total. [OK]
- Saying O(nĆk) by not accounting for amortized analysis
- Saying O(n log n) confusing with heap-based approach
- Not knowing each element is pushed/popped at most once total
Solution
Step 1: Why pop from back
If nums[deque.back()] <= nums[i], the back element can never be the window maximum while nums[i] is in the window. nums[i] is larger AND will leave the window later.Step 2: Keep decreasing order
Pop all elements from back where nums[back] <= nums[i], then push i. This maintains the monotonic decreasing invariant.Final Answer:
Pop back when nums[deque.back()] <= nums[i]. -> Option BQuick Check:
Back pop: current element >= back element ā back is useless, remove it. [OK]
- Confusing front pop (out-of-window check) with back pop (useless element removal)
- Popping only when strictly less than, missing equals case
- Thinking deque size triggers back pop
Solution
Step 1: Window boundary
The current window is [i-k+1, i]. The front of deque holds the maximum's index. If that index <= i-k, it's outside the window.Step 2: Pop expired maximum
Pop front when deque.front() <= i - k. The front is no longer in the current window - the next front becomes the new maximum.Final Answer:
Pop front when front index <= i - k (out of window). -> Option BQuick Check:
Front pop: index out of window (front <= i-k). Back pop: value useless (back <= current). [OK]
- Confusing front pop condition with back pop condition
- Using < i-k instead of <= i-k (off by one)
- Thinking front pop is based on value comparison
Ready to prove it?
Pass the test and this pattern moves unknown
