Problem 308377 · easy · Phase 03 Linear Management & Searching

First Negative in Each Window

sliding window · deque · fixed-size window

For every block of k consecutive numbers in nums, report the first negative number in that block (the one with the smallest index). If a block contains no negative number, report 0 for it. Return the reports in order, one per window.

Examples

Input:  nums = [-8, 2, 3, -6, 10], k = 2
Output: [-8, 0, -6, -6]

Input:  nums = [12, -1, -7, 8, -15, 30, 16, 28], k = 3
Output: [-1, -1, -7, -15, -15, 0]

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…