Discover how to find the highest number in every group without checking each group again and again!
Why Sliding Window Maximum Using Deque in DSA Python?
Imagine you have a long list of daily temperatures, and you want to find the highest temperature for every 3-day period. Doing this by checking each 3-day group one by one feels like repeating the same work over and over.
Manually checking each group means looking at many numbers again and again. This takes a lot of time and can easily cause mistakes, especially if the list is very long.
Using a sliding window with a deque helps keep track of the highest numbers efficiently. It moves through the list just once, updating the highest value quickly without rechecking everything.
for i in range(len(nums) - k + 1): print(max(nums[i:i+k]))
from collections import deque window = deque() for i in range(len(nums)): # update window and print max
This method lets you find maximum values in moving windows fast, even for huge lists, saving time and effort.
Stock traders use this to find the highest stock price in the last few days quickly, helping them make smart decisions.
Manual checking repeats work and is slow.
Deque keeps track of max values efficiently.
Sliding window moves through data once, saving time.
Practice
Solution
Step 1: Understand the role of deque
A deque stores indices of elements in the current window, maintaining order to quickly find the max.Step 2: Why not other options?
Sorting or reversing is not done; sum is unrelated to max. Deque helps track max efficiently.Final Answer:
To store indices of elements in a way that helps find the maximum efficiently -> Option DQuick Check:
Deque stores indices for max tracking [OK]
- Thinking deque stores actual values instead of indices
- Assuming deque sorts the array
- Confusing deque with sum calculation
Solution
Step 1: Import deque correctly
Deque must be imported from collections module before use.Step 2: Initialize empty deque
Using dq = deque() after import is correct syntax; dq = deque([]) also works but less common.Final Answer:
from collections import deque; dq = deque() -> Option AQuick Check:
Import then dq = deque() [OK]
- Using list [] instead of deque
- Not importing deque before use
- Using collections.deque() without import
from collections import deque
def max_sliding_window(nums, k):
dq = deque()
result = []
for i in range(len(nums)):
while dq and dq[0] <= i - k:
dq.popleft()
while dq and nums[dq[-1]] < nums[i]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(nums[dq[0]])
return result
print(max_sliding_window([1,3,-1,-3,5,3,6,7], 3))Solution
Step 1: Understand sliding window max logic
For each window of size 3, the code finds max by maintaining indices in dq.Step 2: Trace output for input
Windows and max: [1,3,-1] -> 3, [3,-1,-3] -> 3, [-1,-3,5] -> 5, [-3,5,3] -> 5, [5,3,6] -> 6, [3,6,7] -> 7Final Answer:
[3, 3, 5, 5, 6, 7] -> Option AQuick Check:
Sliding window max = [3,3,5,5,6,7] [OK]
- Appending values instead of indices to deque
- Not removing indices out of window
- Confusing window size with output length
from collections import deque
def max_sliding_window(nums, k):
dq = deque()
result = []
for i in range(len(nums)):
while dq and nums[dq[-1]] <= nums[i]:
dq.pop()
dq.append(i)
if dq[0] == i - k:
dq.popleft()
if i >= k - 1:
result.append(nums[dq[0]])
return result
print(max_sliding_window([1,3,-1,-3,5,3,6,7], 3))Solution
Step 1: Check condition for removing old indices
Indices outside the window must be removed when dq[0] <= i - k (equivalent to dq[0] < i - k + 1).Step 2: Analyze given condition
Given condition dq[0] == i - k removes only if exactly equal, missing older indices or multiples; must use while loop too.Final Answer:
The condition to remove old indices should be dq[0] <= i - k -> Option BQuick Check:
Remove indices outside window with dq[0] <= i - k [OK]
- Using equality instead of less than for removal
- Removing indices too late causing wrong max
- Not removing old indices at all
nums = [4, 2, 12, 3, 8, 7, 9] and window size k = 4, what is the output of the sliding window maximum using deque approach?Solution
Step 1: Identify windows and their max values
Windows of size 4: [4,2,12,3] max=12, [2,12,3,8] max=12, [12,3,8,7] max=12, [3,8,7,9] max=9.Step 2: Verify max values carefully
Third window max is 12, but 12 is at index 2, window covers indices 2-5: 12,3,8,7 max=12. Fourth window max is 9.Final Answer:
[12, 12, 12, 9] -> Option CQuick Check:
Sliding window max for k=4 = [12,12,12,9] [OK]
- Mixing up window boundaries
- Choosing wrong max by ignoring window size
- Confusing values with indices
Ready to prove it?
Pass the test and this pattern moves unknown
