Steady stretch: hints, no solution

Medium · Sliding window

A solar inverter reports its output once a minute as an integer number of watts. Operators call a stretch of consecutive readings steady when the highest and the lowest reading inside it differ by at most a tolerance.

Return the length of the longest steady stretch. It is not enough for neighbouring readings to be close to each other: the gap between the extremes of the entire stretch is what counts. Any single reading is a steady stretch by itself, so the answer is at least 1 for a non-empty list and 0 for an empty one.

Examples

Input
readings = [9, 4, 6, 5, 7, 12], tolerance = 3
Output
4
Note
The stretch 4, 6, 5, 7 has extremes 4 and 7, which differ by 3.
Input
readings = [1, 2, 3, 4, 5], tolerance = 1
Output
2
Note
Readings rise by one each time, but a stretch of three already differs by 2.
Input
readings = [8, 8, 8], tolerance = 0
Output
3
Note
Equal readings are always steady.

Constraints

  • 0 <= readings.length <= 300000
  • 0 <= readings[i] <= 10000
  • 0 <= tolerance <= 10000

Hint 1: a nudge

For a stretch to be steady, which two readings inside it decide everything?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first