Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Valid Mountain Array


Solution


    class Solution {
    public:
        bool validMountainArray(vector<int>& arr) {
            if(arr.size() < 3)
                return false;
            int i = 0;
            while(i < arr.size() - 1 && arr[i] < arr[i + 1])
                i++;
            if(i == arr.size() - 1 || i == 0)
                return false;
            while(i < arr.size() - 1 && arr[i] > arr[i + 1])
                i++;
            if(i == arr.size() - 1)
                return true;
            return false;
        }
    };