Sliding Window Peaks
A monitoring dashboard shows the peak value over the last k samples. Given the array samples and window size k, return an array with the maximum of every contiguous window of length k, from left to right.
Aim for O(n) total time.
Example 1
Input: samples = [1, 3, -1, -3, 5, 3, 6, 7], k = 3 Output: [3, 3, 5, 5, 6, 7]
Example 2
Input: samples = [4], k = 1 Output: [4]
Constraints
- 1 <= k <= samples.length <= 10^5
- -10^4 <= samples[i] <= 10^4
Hint 1
Keep a deque of indices whose values are in decreasing order.
Hint 2
Drop indices from the back that are smaller than the new value, and from the front once they leave the window.
Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer, Site Reliability Engineer, Data Engineer
Run checks your code on 2 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.