#423 · Sliding window · Advanced · 160 XP · about 30 min
A weather station logs the wind speed once a minute, and readings[i] is the i-th reading. A stretch of consecutive readings is calm when its highest reading minus its lowest reading is at most limit.
Write longest_calm(readings, limit) that returns the length of the longest calm stretch, or 0 if there are no readings.
longest_calm([12, 15, 11, 30, 28, 33, 29, 27], 6) # returns 5 longest_calm([3, 9, 4, 6, 5, 12], 3) # returns 3 longest_calm([5, 5, 5, 5], 0) # returns 4
In the first example the stretch 30, 28, 33, 29, 27 is calm: its highest reading is 33 and its lowest is 27, a difference of 6.
0 <= len(readings) <= 100,0000 <= readings[i] <= 10^90 <= limit <= 10^9Working out the highest and lowest reading of every stretch from scratch is far too slow for 100,000 readings.
First solves only, ranked by stars and then time.