The peak in each window
A monitoring chart smooths readings by asking, for each fixed-size window, what the loudest moment inside it was.
- Each output entry is the largest value inside one contiguous window of size `window`.
- The windows slide by one position at a time.
- A window of zero or less, or one larger than the whole list, gives an empty result.
window_max(values: list<int>, window: int) → list<int>
Where you start
def window_max(values: list[int], window: int) -> list[int]:
Worked examples
| Call | Result |
|---|---|
window_max([1, 3, 2, 5], 2) | [3, 3, 5] |
window_max([1, 2, 3], 3) | [3] |
window_max([5], 1) | [5] |
window_max([4, 1, 3], 2) | [4, 3] |
Hint
For each starting index take the maximum of the next `window` values with an inner loop.
Reference solution in Python
def window_max(values: list[int], window: int) -> list[int]:
result = []
if window <= 0 or window > len(values):
return result
for i in range(len(values) - window + 1):
m = values[i]
for j in range(i + 1, i + window):
if values[j] > m:
m = values[j]
result.append(m)
return result