Huhu

 avatar
unknown
c_cpp
a year ago
956 B
3
Indexable
class Solution {
public:
    bool increase (vector<int> nums) {
        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] > nums[i + 1]) 
                return false;
        }

        return true;
    }

    bool decrease (vector<int> nums) {
        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] < nums[i + 1]) 
                return false;
        }

        return true;
    }

    bool isMonotonic(vector<int>& nums) {
        bool check;

        int diff = 0; int cnt = 0;

        while (diff == 0) {
            diff = nums[cnt + 1] - nums[cnt];
            cnt++;

            if (cnt > nums.size() - 1) {
                check = 1;
                return check;
            }
        }

        if (diff > 0) { // tăng
            check = increase(nums);
        }
        else {
            check = decrease(nums);
        }

        return check;
    }
};
Leave a Comment