[LeetCode] Valid Mountain Array

941. Valid Mountain Array

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

  • There exists some 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]

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool validMountainArray(vector<int>& arr) {
int i = 0;
for(; i < arr.size() - 1 and arr[i] < arr[i+1]; i++) {}
if(i == arr.size() - 1) return false;
if(i == 0) return false;
for(; i < arr.size() - 1 and arr[i] > arr[i+1]; i++) {}
return i == arr.size() - 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/09/PS/LeetCode/valid-mountain-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.