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 you have a list of numbers and a special magnifying glass that can only look at a small, fixed number of them at a time. The brute force approach is to slide this magnifying glass over the entire list, one number at a time, and for each position, we meticulously find the biggest number visible inside the glass.
Here's how the algorithm would work step-by-step:
def sliding_window_maximum_brute_force(numbers_list, window_size):
maximums_in_windows = []
number_of_windows = len(numbers_list) - window_size + 1
# Iterate through all possible starting positions for the sliding window.
for window_start_index in range(number_of_windows):
window_end_index = window_start_index + window_size
current_window = numbers_list[window_start_index:window_end_index]
# For each window, we must find the maximum value within that specific slice of the list.
current_maximum_value = current_window[0]
for number_in_window in current_window:
if number_in_window > current_maximum_value:
current_maximum_value = number_in_window
# After finding the max for the current window, add it to our list of results.
maximums_in_windows.append(current_maximum_value)
return maximums_in_windowsThe core idea is to cleverly keep track of potential candidates for the maximum value in a special, ordered list. Instead of re-scanning every number in the window, we maintain a shortlist of the best numbers we've seen recently, which lets us find the maximum for each new window almost instantly.
Here's how the algorithm would work step-by-step:
from collections import deque
def sliding_window_maximum(numbers_list, window_size):
max_values_for_each_window = []
candidate_indices_deque = deque()
start_of_window = 0
for end_of_window in range(len(numbers_list)):
# Remove smaller numbers from the back as they can no longer be the maximum.
while candidate_indices_deque and numbers_list[candidate_indices_deque[-1]] < numbers_list[end_of_window]:
candidate_indices_deque.pop()
candidate_indices_deque.append(end_of_window)
# Remove the leftmost element if it's out of the current window's bounds.
if start_of_window > candidate_indices_deque[0]:
candidate_indices_deque.popleft()
# The deque's front is the max for the current window once it's fully formed.
if (end_of_window + 1) >= window_size:
max_values_for_each_window.append(numbers_list[candidate_indices_deque[0]])
start_of_window += 1
return max_values_for_each_window| Case | How to Handle |
|---|---|
| Empty input array | The function should return an empty array as no windows can be formed. |
| Window size k is larger than the array size | Return an empty array because a full window of size k can never be formed. |
| Window size k is 1 | The maximum of each window is just the element itself, so the original array should be returned. |
| Array contains all identical elements | The maximum for every window will be the same, so the result array should contain that value repeated for each window. |
| Array contains negative numbers, zeros, and positive numbers | The comparison logic must correctly handle the full range of integer values to find the true maximum in each window. |
| Window size k is equal to the array size | There is only one window, so the result should be a single-element array containing the maximum of the entire input array. |
| Very large input array (e.g., 10^5 elements) | A solution using a deque or a similar O(n) approach is required to avoid a timeout that would occur with a naive O(n*k) solution. |
| Array elements are in strictly decreasing order | An efficient deque-based solution will correctly identify the first element of each window as its maximum. |