Count Elements Larger Than Neighbours
EasyP3Coding
Return the number of elements of nums that are strictly greater than every adjacent element. The first and last elements have one neighbour; every other element has two.
An element with no neighbours (a one-element array) counts, since nothing is larger than it. For an empty array return 0.
Example 1
- Input
- nums = [1,3,2,4,1]
- Output
- 2
- Why
- 3 and 4 are larger than their neighbours; 1 at the ends is not.
Example 2
- Input
- nums = [5]
- Output
- 1
Example 3
- Input
- nums = []
- Output
- 0
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.