Skip to the code
cf.
+50 XP

#139 Longest Steady Temperature Stretch

hard

A smart-home dashboard highlights the longest stretch of readings where the temperature stayed steady: the highest and lowest reading in the stretch differ by at most tolerance.

Write longestSteady(readings, tolerance) that returns the length of the longest run of neighboring readings whose maximum minus minimum is at most tolerance. Return 0 for an empty list.

Example 1

Input: readings = [8,2,4,7], tolerance = 4
Output: 2

[2, 4] and [4, 7] stay within 4; any run of three spans at least 5.

Example 2

Input: readings = [10,1,2,4,7,2], tolerance = 5
Output: 4

[2, 4, 7, 2] ranges from 2 to 7.

Constraints

  • 0 <= readings.length <= 100000
  • -10^4 <= readings[i] <= 10^4
  • 0 <= tolerance <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).