6-liner O(N) time and O(1) space


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

Log in to reply
 

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