Sliding Window Maximum
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write windowMaxes(nums, k): slide a window of k numbers in a row along nums, one step at a time from the start to the end, and return an array of the largest number in each window, in order, so there are nums.length minus k plus 1 of them. k is a whole number from 1 to nums.length, so nums is never empty. For example, windowMaxes([1, 3, -1, -3, 5, 3, 6, 7], 3) is [3, 3, 5, 5, 6, 7]. The checks include 100,000 numbers with k = 50,000, so scanning every window is too slow.
Your code must pass
- Finds the largest number in each window
- Returns one number per window
- Works when k is 1 or the whole array
- Handles negative and repeated numbers
- Is fast enough for 100,000 numbers