Skip to the code
+25 XP

#599 Longest Rise and Fall in a Chart

medium

A traffic chart highlights its longest "spike": a run of neighboring points that strictly rises to a peak and then strictly falls. A spike has at least 3 points, with at least one point before the peak and at least one after it.

Write longestSpike(points) that returns the number of points in the longest spike, or 0 if there is none.

Example 1

Input: points = [2,1,4,7,3,2,5]
Output: 5

1, 4, 7, 3, 2 rises to 7 and falls.

Example 2

Input: points = [2,2,2]
Output: 0

Flat points never rise or fall.

Constraints

  • 0 <= points.length <= 2 * 10^5
  • 0 <= points[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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