FunctionRacerProblemsRace

Longest Steady Stretch

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

A stretch is a run of consecutive items of nums. Call it steady when the difference between its largest and its smallest item is at most limit. Return the length of the longest steady stretch.

A single item is always a steady stretch, so the answer is at least 1.

Example 1

nums [8,2,4,7]
limit 4
returns 2

[2,4] has a spread of 2 and [4,7] of 3; adding 8 or 2 to either pushes the spread past 4.

Example 2

nums [10,1,2,4,7,2]
limit 5
returns 4

[1,2,4,7] spans 1 to 7, a spread of 6, which is too much; [2,4,7,2] spans 2 to 7 and is the longest steady stretch.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems