Given an array of integers arr, return True if and only if it is a valid mountain array.
arr is a mountain array if and only if len(arr) >= 3 and there is some index i with 0 < i < len(arr) - 1 such that:
-
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
-
arr[i] > arr[i + 1] > ... > arr[len(arr) - 1]
Both comparisons are strict, so two equal neighboring values anywhere in the array mean it is not a mountain.
Function Signature
def valid_mountain_array(arr: list[int]) -> bool:
Constraints
-
1 <= len(arr) <= 10^4
-
0 <= arr[i] <= 10^4
Examples
Input: arr = [2,1]
Output: False
The array has fewer than 3 elements, so it cannot be a mountain.
Input: arr = [3,5,5]
Output: False
The values rise from 3 to 5, but the two 5s are equal, so the part after the peak is not strictly decreasing.
Input: arr = [0,3,2,1]
Output: True
The values rise strictly from 0 to 3 at index 1, which is neither the first nor the last index, and then fall strictly from 3 to 1.