First Value Below Both Neighbors
Problem statement
Given an integer array nums, inspect its interior elements from left to right. Return the value of the first element whose immediate left and right neighbors are both strictly larger.
Return -1 when no such interior element exists. The first and last elements are not eligible because they do not have two adjacent neighbors.
Function
firstValleyValue(nums: int[]) → intExamples
Example 1
nums = [8,3,5,2,4]return = 3The value 3 is below both 8 and 5. Although 2 is also below both neighbors, it appears later.
Example 2
nums = [1,2,3]return = -1The only interior value is not below either neighbor.
Example 3
nums = [5,1,1,5]return = -1Equal adjacent values do not satisfy the strict comparison.
Constraints
3 <= nums.length <= 10^5.0 <= nums[i] <= 10^9.