The launch window
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- indexes
Problem
The launch is weeks away, and mission control already has the forecast: the wind speed, one number per day. To fuel up and lift off, the rocket needs k calm days in a row. A day is calm if its wind is lower than the limit; a wind equal to the limit is not calm, and a single bad day breaks the streak.
Write a function that takes the list of winds (whole numbers, 0 or greater), the limit and k (a whole number greater than 0). Return the position of the day where the first streak of k calm days begins, not the day where it is completed. Positions start at 0.
With [9, 3, 8, 2, 4, 1, 7], limit 5 and k 3, days 3, 4 and 5 are calm: you return 3. If there is no such streak, or the list is empty, you return -1.
Examples
The example
[9, 3, 8, 2, 4, 1, 7], 5, 3 → 3
Calm from the first day
[1, 2, 3, 9], 5, 3 → 0
One streak breaks and another starts later
[2, 3, 7, 1, 2, 4, 6], 5, 3 → 3
Equal to the limit is not calm
[4, 5, 4, 4, 3], 5, 3 → 2
Never three days in a row
[6, 1, 1, 8, 1, 1], 5, 3 → -1
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def first_day(winds, limit, k):
passJavaScript
function firstDay(winds, limit, k) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.