Problem 308377 · easy · Level 03 Linear Management & Searching

First Frost in Each Run of Days

sliding window · deque · fixed-size window

A weather station logs one temperature per day in nums. For every run of k consecutive days, a gardener wants that run's first frost: the earliest temperature in it that is below zero (not the coldest one). A run with no frost at all is reported as 0. Return the reports in order, one per run of k days.

Examples

Input:  nums = [3, -1, 2, 4, -5, 6], k = 2
Output: [-1, -1, 0, -5, -5]
Explanation: the days [2, 4] have no frost, so that run reports 0.

Input:  nums = [-3, 9, 8, -4, -6, 1, 2, 7], k = 3
Output: [-3, -4, -4, -4, -6, 0]
Explanation: in [8, -4, -6] the first frost is -4, even though -6 is colder.

Constraints

  • 1 <= k <= len(nums) <= 10**5
  • -10**9 <= nums[i] <= 10**9
  • Target complexity: O(n) time overall; scanning each window separately is too slow for the largest tests.

Goals

  • Keep a queue of candidate indices that are still inside the window
  • Discard indices that fall out of the window from the front
Starting Python…