Given an integer array
arr.
Return true if the array is a mountain array. Otherwise return false.An array is a mountain array if and only if:
arr.length >= 3- There exists an index
isuch that0 < i < arr.length - 1, and:arr[0] < arr[1] < ... < arr[i - 1] < arr[i]arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Example 1
Input: arr = [0,1,2,1,0]
Output: true
Explanation: Ascent [0,1,2] followed by descent [2,1,0] — a valid mountain sequence.
Output: true
Explanation: Ascent [0,1,2] followed by descent [2,1,0] — a valid mountain sequence.
Example 2
Input: arr = [2,1]
Output: false
Explanation: Length is less than 3 — cannot be a mountain, return false.
Output: false
Explanation: Length is less than 3 — cannot be a mountain, return false.
Example 3
Input: arr = [0,3,2,1]
Output: true
Explanation: The array rises to a peak (3), then descends — this is a mountain array.
Output: true
Explanation: The array rises to a peak (3), then descends — this is a mountain array.