Problem 375014 · medium · Phase 03 Linear Management & Searching

Peak Reading per Window

sliding window · monotonic deque · fixed-size window

A weather station logs one temperature per hour in temps. For every block of k consecutive hours, report the highest temperature in that block. Return the list of peaks, one per window, from left to right.

Examples

Input:  temps = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output: [3, 3, 5, 5, 6, 7]

Input:  temps = [1], k = 1
Output: [1]

Constraints

  • 1 <= k <= len(temps) <= 10**5
  • -10**9 <= temps[i] <= 10**9
  • Target complexity: O(n) time. Calling max on every window is O(n·k) and too slow for the largest tests.

Goals

  • Maintain a deque of candidate indices in decreasing value order
  • Evict indices that leave the window and values dominated by a newer element
Starting Python…