1677. First Cold Day Each Week
A weather station logged one temperature per day in temps. For every window of k consecutive days, from the window starting at day 0 to the window ending at the last day, report the first cold day: the leftmost index inside that window whose temperature is strictly below threshold, or -1 when no day in the window is that cold.
Return an array of n - k + 1 answers in window order, where n = temps.length. Keep a queue of indices of cold days; add each cold day as it appears, and drop indices from the front once they fall out of the current window, so the front is always the answer. This runs in O(n) time and O(n) space. Constraints: 1 <= k <= n <= 1000 and -100 <= temps[i], threshold <= 100.
Example 1
- Input:
- temps = [5,-3,8,2,-1,7], k = 3, threshold = 0
- Output:
- [1,1,4,4]
- Explanation:
Windows start at 0..3, and their leftmost readings below 0 are at indices 1, 1, 4 and 4.
Example 2
- Input:
- temps = [10,20,30], k = 2, threshold = 5
- Output:
- [-1,-1]
- Explanation:
No reading is below 5, so every window answers -1.
Example 3
- Input:
- temps = [-5,-6,-7], k = 1, threshold = -6
- Output:
- [-1,-1,2]
- Explanation:
Only -7 is strictly below -6, so just the last single-day window has an answer.
Constraints
1 ≤ k ≤ temps.length ≤ 1000; -100 ≤ temps[i], threshold ≤ 100
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)