C++ easy and straight-forward O(n) solution


  • 0
    M
        bool checkPossibility(vector<int>& nums) {
            if(nums.size()<3) return true;
            int ct=0;
            if(nums[0]>nums[1]) {
                nums[0]=nums[1];
                ct++;
            }
            for(int i=1;i<nums.size()-1;i++) {
                if(nums[i]>nums[i+1]) {
                    if(ct==1) return false;
                    if(nums[i+1]>=nums[i-1]) nums[i]=nums[i-1];
                    else nums[i+1]=nums[i];
                    ct++;
                }
            }
            return nums[nums.size()-1]>=nums[nums.size()-2];
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.