113. Sliding Window Maximum

HardArrayArray

You are given an integer array nums and an integer k. A sliding window of size k moves from left to right. Return the maximum value in the window at each step.

Input: Integer array nums and integer k (window size).

Output: Array of maximum values for each window position.

Examples

Example 1
Input: [1,3,-1,-3,5,3,6,7],3
Output: [3,3,5,5,6,7]
Explanation: Windows: [1,3,-1]→3, [3,-1,-3]→3, [-1,-3,5]→5, [-3,5,3]→5, [5,3,6]→6, [3,6,7]→7.
Example 2
Input: [1],1
Output: [1]
Explanation: Single window.
Example 3
Input: [1,2,3,4,5],1
Output: [1,2,3,4,5]
Explanation: k=1: each element is its own window max.

Constraints

Asked by

OracleAmazonMicrosoftGoogleMetaBloomberg
Solve this problem in the editor →