A theater's lighting board logs the brightness level set for a spotlight at each cue during a performance, recorded in order as an array of n integers. The operator sometimes holds the same brightness across several consecutive cues, so cues with a brightness equal to their immediate neighbor belong to the same plateau and should never be double counted as separate extremes.
For a given cue, look at the nearest cue before it whose brightness differs (skipping over any run of equal values) and the nearest cue after it whose brightness differs. A cue (or, more precisely, an entire plateau of equal-brightness cues) is a flare if its brightness is strictly greater than both of those differing neighbors, and a dip if its brightness is strictly less than both of those differing neighbors. A plateau touching either end of the array, so that no differing neighbor exists on one side, is neither a flare nor a dip. Each qualifying plateau is counted exactly once, no matter how many cues it spans.
Count the total number of flares plus dips.
Print a single integer: the total number of flare cues plus dip cues.
Example 1
Input
9 3 3 5 1 1 6 2 2 4
Expected
4
Explanation
Collapsing consecutive equal readings gives the plateau sequence 3, 5, 1, 6, 2, 4. Checking each interior plateau against its differing neighbors: 5 (neighbors 3 and 1) is a flare, 1 (neighbors 5 and 6) is a dip, 6 (neighbors 1 and 2) is a flare, and 2 (neighbors 6 and 4) is a dip. That is 2 flares + 2 dips = 4.
Example 2
Input
7 1 2 3 3 3 4 5
Expected
0
Explanation
Collapsing the plateau of three 3s gives 1, 2, 3, 4, 5, a strictly increasing sequence. Every interior value has one neighbor smaller and one neighbor larger, so no plateau is a flare or a dip. The answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →