Skip to the code
cf.
+10 XP

#177 Peak of an Easing Curve

easy

An animation editor samples an easing curve that overshoots: the sampled values in values rise strictly to a single highest point and then fall strictly. Either part may be empty, so the peak can be the first or last sample.

Write peakFrame(values) that returns the index of the highest sample. Use O(log n) comparisons; ternary search is a natural fit.

Example 1

Input: values = [1,3,8,12,15,11,6,2]
Output: 4

The values climb to 15 at index 4, then fall.

Example 2

Input: values = [9,7,4,1]
Output: 0

The curve only falls, so the first sample is the peak.

Constraints

  • 1 <= values.length <= 100000
  • Values rise strictly to one peak, then fall strictly.
  • -10^6 <= values[i] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).