Leetcode 665 Solution

This article provides solution to leetcode question 665 (non-decreasing-array)

https://leetcode.com/problems/non-decreasing-array

Solution

class Solution {
public:
    bool checkPossibility(vector<int>& nums) {
        int state = 0;
        int last_val = -100000;

        for (int i = 0; i < nums.size(); i++)
        {
            int val = nums[i];

            if (val < last_val)
            {
                if (state != 0)
                    return false;

                if (i == 1 || val >= nums[i - 2])
                    last_val = val;

                state = 1;
            }
            else
                last_val = val;
        }

        return true;
    }
};