Given an array of integers and an integer k, find the maximum element in each window of size k.
Explanation: Step-by-step: with input [1, 3, -1, -3, 5, 7, 8, 12], we initialize a deque to store indices of the maximum elements in the current window. We iterate through the array, and for each element, we remove the indices of elements that are out of the current window from the deque. We then add the index of the current element to the deque and update the maximum element in the current window. We append the maximum element in the current window to the result array. Finally, we return the result array.
Explanation: Step-by-step: with input [5, 7, 8, 12, 15], we initialize a deque to store indices of the maximum elements in the current window. We iterate through the array, and for each element, we remove the indices of elements that are out of the current window from the deque. We then add the index of the current element to the deque and update the maximum element in the current window. We append the maximum element in the current window to the result array. Finally, we return the result array.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak