FunctionRacerProblemsRace

Evenly Spaced Runs

EasyDynamic programmingPython, JavaScript, TypeScript, Java, C++

A run is a stretch of at least three consecutive entries of readings in which every two neighbors differ by the same amount, such as [7, 5, 3] or [4, 4, 4].

Return how many runs readings contains. Runs that overlap, or that lie inside a longer run, all count separately. A list with fewer than three entries has no runs.

Example 1

readings [1,3,5,7,6]
returns 3

The runs are 1, 3, 5, 3, 5, 7 and 1, 3, 5, 7. The 6 breaks the pattern.

Example 2

readings [4,1,-2,0,2,4]
returns 4

4, 1, -2 falls by 3 each time. -2, 0, 2, 4 rises by 2 and holds three runs: two of length 3 and one of length 4.

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 Dynamic programming problems