Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Given an array of integers arr
, return true
if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if:
arr.length >= 3
i
with 0 < i < arr.length - 1
such that:arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Example 1:
Input: arr = [2,1]
Output: false
Example 2:
Input: arr = [3,5,5]
Output: false
Example 3:
Input: arr = [0,3,2,1]
Output: true
Constraints:
1 <= arr.length <= 104
0 <= arr[i] <= 104
bool validMountainArray(vector<int>& A) {
int n = A.size(), i = 0, j = n - 1;
while (i + 1 < n && A[i] < A[i + 1]) i++;
while (j > 0 && A[j - 1] > A[j]) j--;
return i > 0 && i == j && j < n - 1;
}
public boolean validMountainArray(int[] A) {
int n = A.length, i = 0, j = n - 1;
while (i + 1 < n && A[i] < A[i + 1]) i++;
while (j > 0 && A[j - 1] > A[j]) j--;
return i > 0 && i == j && j < n - 1;
}
def validMountainArray(self, A):
i, j, n = 0, len(A) - 1, len(A)
while i + 1 < n and A[i] < A[i + 1]: i += 1
while j > 0 and A[j - 1] > A[j]: j -= 1
return 0 < i == j < n - 1
In our experience, we suggest you solve this Valid Mountain Array LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Valid Mountain Array LeetCode Solution
I hope this Valid Mountain Array LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>