FastPrepPeak Elements in Removal Order

Peak Elements in Removal Order

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

Repeatedly remove one peak from the current array until it is empty. An interior element is a peak when it is not smaller than either current neighbor. An endpoint is a peak when it is not smaller than its only neighbor, and the sole remaining element is a peak.

At each step remove the leftmost current peak. Return the removed values in order.

Function

removePeaks(values: int[]) → int[]

Examples

Example 1

values = [1,3,2,4]return = [3,4,2,1]

Peaks are recomputed after each leftmost removal.

Example 2

values = [7]return = [7]

The sole element is a peak.

Constraints

  • 0 <= values.length <= 2000
  • -1000000000 <= values[i] <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[] removePeaks(int[] values) {
    // Write your code here.
}
values[1,3,2,4]
expected[3,4,2,1]
Checking account…