Loading...
Loading...
Loading...
A sensor sends n readings. A stretch is steady when its highest and lowest readings differ by at most a limit. Print the length of the longest steady stretch.
Example
Input
4 4 8 2 4 7
Output
2
The readings 2 4 differ by 2, and 4 7 by 3. No stretch of 3 is within 4, so the answer is 2.
Try the problem first. Open one hint at a time.
Keep two deques: one for the current maximum and one for the current minimum.
While the maximum minus the minimum is above the limit, move the left end and pop the front of a deque when its value leaves.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor