668. Sliding Window Minimum and Maximum Together

HardStackDequeMonotonicSliding Window

Given an array nums and a window size k, return for each contiguous window of size k a pair [min, max] of its minimum and maximum, using two monotonic deques. The input is JSON {nums, k}.

Input: JSON {nums, k}.

Output: Array — a [min, max] pair for each window.

Examples

Example 1
Input: {"nums":[1,3,-1,-3,5],"k":3}
Output: [[-1,3],[-3,3],[-3,5]]
Explanation: Min and max of each size-3 window.
Example 2
Input: {"nums":[4],"k":1}
Output: [[4,4]]
Explanation: One element.
Example 3
Input: {"nums":[2,1,5,3],"k":2}
Output: [[1,2],[1,5],[3,5]]
Explanation: Pairwise windows.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →