621. Sliding Window Maximum (Monotonic Deque)

MediumStackDequeMonotonicSliding Window

Given an array nums and a window size k, return the maximum of each contiguous window of size k as it slides from left to right, using a monotonic deque for O(n) time. The input is JSON {nums, k}.

Input: JSON {nums, k}.

Output: Array — the maximum of each window.

Examples

Example 1
Input: {"nums":[1,3,-1,-3,5,3,6,7],"k":3}
Output: [3,3,5,5,6,7]
Explanation: Maximum of each size-3 window.
Example 2
Input: {"nums":[1],"k":1}
Output: [1]
Explanation: One window.
Example 3
Input: {"nums":[9,8,7],"k":2}
Output: [9,8]
Explanation: Decreasing values.

Constraints

Asked by

OracleAmazonMicrosoftGoogleMetaBloomberg
Solve this problem in the editor →