clueless coding // TODO: be smarter

LeetCode Non-decreasing Array: CPP Solution



TL;DR Code Solution


bool checkPossibility(vector<int>& nums) {
    int cnt = 0;                                                                    //the number of changes
    for(int i = 1; i < nums.size() && cnt<=1 ; i++){
        if(nums[i-1] > nums[i]){
            cnt++;
            if(i-2<0 || nums[i-2] <= nums[i])nums[i-1] = nums[i];                    //modify nums[i-1] of a priority
            else nums[i] = nums[i-1];                                                //have to modify nums[i]
        }
    }
    return cnt<=1;
}