Leetcode 978 Solution

This article provides solution to leetcode question 978 (valid-mountain-array)

https://leetcode.com/problems/valid-mountain-array

Solution

class Solution {
public:
    bool validMountainArray(vector<int>& A) {
        if (A.size() < 3)
            return false;

        bool up = false;
        bool down = false;

        for (int i = 1; i < A.size(); i++)
        {
            if (A[i] > A[i - 1])
            {
                if (down)
                    return false;
                up = true;
            }
            else if (A[i] < A[i - 1])
            {
                if (up == false)
                    return false;
                down = true;
            }
            else
                return false;
        }

        return up && down;
    }
};