Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Sliding Window Maximum

Hard
sliding-windowdequeheapmonotonic-stack

Given an integer array nums and a window size k, return the maximum of each contiguous window of size k as it slides left to right.

Input

  • Line 1: space-separated integers nums
  • Line 2: integer k

Output: the window maximums, space-separated.

Example 1
Input:
1 3 -1 -3 5 3 6 7
3
Output:
3 3 5 5 6 7

Windows: [1,3,-1] [3,-1,-3] [-1,-3,5] [-3,5,3] [5,3,6] [3,6,7].

Example 2
Input:
1
1
Output:
1

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10^5
  • -10^4 ≤ nums[i] ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.