Sliding window · Hard
A monitoring display receives a sequence of integer measurements. For every complete group of k adjacent readings it needs the peak, in time order. Recomputing the peak from scratch is too slow for large windows.
Return the maximum of each length-k contiguous window in linear total time without changing the measurements.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "nums": number[], "k": number }.
Fields
- nums contains measurements in their original order.
- k is an integer window size; invalid sizes are part of the contract.
Output
Return value
- Return an array in increasing window-start order.
- If k < 1 or k > nums.length return []. Otherwise return nums.length-k+1 values.
- Equal peaks at different indexes must expire at the correct times.
Constraints
- At most 100000 values, each an integer in [-1000000000,1000000000].
- -100000 <= k <= 100001.
Examples
Example 1
{
"k": 3,
"nums": [
1,
3,
3,
2,
5
]
}Returns
[
3,
3,
5
]Three complete windows.
Example 2
{
"k": 1,
"nums": [
-2,
-5
]
}Returns
[
-2,
-5
]A single-entry window preserves each value.
Required complexity
O(n) time and O(k) working space for valid k, excluding output