Back to DSA

Sliding Window Maximum

hard
Acceptance: 42%
ArraysSliding Window

An integer array and a window size k are given. A window of exactly k consecutive elements slides across the array one position at a time, from left to right. For each window position, report the maximum value within that window. Return all these maximum values as an array.

Examples

Example 1:
Input:nums = [4,2,5,1,3,7,2], k = 3
Output:[5,5,5,7,7]
Explanation: Windows: [4,2,5]->5, [2,5,1]->5, [5,1,3]->5, [1,3,7]->7, [3,7,2]->7.
Example 2:
Input:nums = [9], k = 1
Output:[9]
Explanation: A window of size 1 always contains just the element itself.

Hints

00:00
1234567