FunctionRacerProblemsRace

Raise Stretches to Match

HardArraysPython, JavaScript, TypeScript, Java, C++

Start with a list of zeros that is as long as target. One move picks a stretch of positions next to each other (a single position counts too) and adds 1 to every item in it.

Return the fewest moves that turn the zeros into target.

Example 1

target [1,2,3,2,2]
returns 3

Raise all five positions, then the last four, then the middle one: 3 moves.

Example 2

target [3,1,5]
returns 7

The first item needs 3 moves that cover it. The last item is higher than its neighbor by 4, so 4 more moves must start there.

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 Arrays problems