Problems / Sliding Window Maximum
Hard

Sliding Window Maximum

Hard 0 submissions 50 coins +200 XP
Array Heap (Priority Queue) Sliding Window
Problem Description
## Problem You are given an array of integers `nums` and an integer `k`. There is a sliding window of size `k` moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. Return the **maximum value in each window** as an array. ## Examples **Example 1:** ``` Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3, 3, 5, 5, 6, 7] Explanation: Window position Max [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7 ``` **Example 2:** ``` Input: nums = [1], k = 1 Output: [1] ``` **Example 3:** ``` Input: nums = [1,-1], k = 1 Output: [1, -1] ``` ## Approach Hints Use a monotonic deque (double-ended queue) that stores indices. Maintain it so values at the front are always the largest. Remove indices that are out of the window from the front, and remove indices from the back whose values are smaller than the current element.
Constraints
- `1 <= nums.length <= 100000` - `-10000 <= nums[i] <= 10000` - `1 <= k <= nums.length`

Need help?

Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.

Video Call Whiteboard Live Coding Screen Share