DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
hard
Array

Sliding Window Maximum

**Problem Statement:** You are given an array of integers `nums`, there is a sliding window of size `k` which is 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 max sliding window. **Hint / Expected Approach:** Monotonic deque (decreasing order) **Edge Cases to Consider:** - (1) k = 1 (trivial) - (2) k = n (whole array) - (3) All same elements

Examples

Example 1
Input: [1,3,-1,-3,5,3,6,7], 3
Output: [3,3,5,5,6,7]
Example 2
Input: [1], 1
Output: [1]
Example 3
Input: [1,-1], 1
Output: [1,-1]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪k = 1 (trivial)
  • ▪k = n (whole array)
  • ▪All same elements
Frequently Asked At
GoogleAmazonMicrosoftMeta