You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
Example 1:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] Explanation: Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7
Example 2:
Input: nums = [1], k = 1 Output: [1]
Constraints:
1 <= nums.length <= 105-104 <= nums[i] <= 1041 <= k <= nums.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine looking at a long row of numbers through a small viewing frame that reveals only a few numbers at a time. The brute force strategy involves manually sliding that frame step by step and, at every single stop, looking at all the numbers inside the frame to find the largest one.
Here's how the algorithm would work step-by-step:
def max_sliding_window(nums, window_size):
if not nums or window_size <= 0:
return []
maximum_results = []
# Iterate through all possible starting positions of the sliding window
for start_index in range(len(nums) - window_size + 1):
current_maximum = nums[start_index]
# Inspect every element within the current window to find the largest value
for offset in range(1, window_size):
element_index = start_index + offset
if nums[element_index] > current_maximum:
current_maximum = nums[element_index]
# Store the maximum value found for this specific window position
maximum_results.append(current_maximum)
return maximum_resultsInstead of repeatedly searching for the biggest item in every group, we can maintain a neat line of potential winners as we scan through the sequence. This clever shortcut lets us instantly know the champion of each group without wasting time rechecking items we have already looked at.
Here's how the algorithm would work step-by-step:
from collections import deque
def max_sliding_window(array_of_numbers, window_size):
potential_champion_indices = deque()
maximum_values_result = []
for current_index, current_value in enumerate(array_of_numbers):
# Older smaller items can never win if a larger current item arrives.
while potential_champion_indices and array_of_numbers[potential_champion_indices[-1]] <= current_value:
potential_champion_indices.pop()
potential_champion_indices.append(current_index)
# Remove indices that have fallen out of the current viewing window boundary.
if potential_champion_indices[0] <= current_index - window_size:
potential_champion_indices.popleft()
# Only record the champion once the viewing window has fully formed.
if current_index >= window_size - 1:
maximum_values_result.append(array_of_numbers[potential_champion_indices[0]])
return maximum_values_result| Case | How to Handle |
|---|---|
| Window size k is larger than the array length nums.length | The algorithm should handle this by clamping the window size or returning an empty array since a valid window cannot be formed. |
| Window size k equals 1 | Every element in the array is its own maximum, so the output array should be an exact copy of the input array. |
| Window size k equals the array length nums.length | The sliding window covers the entire array once, resulting in an output array containing a single element which is the maximum of the whole array. |
| Array contains all identical values | The monotonic queue will maintain duplicate indices, but correctly output that same constant value for every window position. |
| Array is strictly decreasing | The monotonic queue stores elements efficiently by removing smaller elements, always keeping the first element of the window as the maximum. |
| Array is strictly increasing | The monotonic queue continually pops older smaller elements from the back, ensuring the newest and largest element is always at the front for each window. |
| Array contains negative numbers and zeros | Using a monotonic queue of indices rather than values ensures we correctly compare signed integers and track positions regardless of magnitude. |
| Extremely large input arrays near memory or time limits | An optimized O(n) deque approach processes each element at most twice, preventing Time Limit Exceeded errors on massive datasets. |