FunctionRacerProblemsRace

Fewest Removals to Leave a Peak

HardArraysPython, JavaScript, TypeScript, Java, C++

A list is a peak when it strictly rises for one or more steps and then strictly falls for one or more steps. So a peak has at least 3 items, and no two neighbors in it are equal.

Remove as few items of heights as you can so that the items left, kept in their order, form a peak. Return how many items you removed, or -1 if no peak can be left.

Example 1

heights [5,2,4,1,6,3]
returns 2

Remove the first 5 and the second 1: what is left is 2, 4, 6, 3.

Example 2

heights [1,2,3,4]
returns -1

The list only rises. Whatever is removed, nothing falls, so there is no peak.

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