Java Solution


  • 0
    public boolean checkPossibility(int[] nums) {
    
        boolean cantGoMore = false;
        if(nums.length > 1 && nums[0] > nums[1]) {
            cantGoMore = true;
            nums[0] = 1;
        }
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] > nums[i]) {
                if (cantGoMore) return false;
                if(i == nums.length-1) return true;
                if (nums[i + 1] - nums[i - 1] <= 1 && nums[i] - nums[i-2] <=1) return false;
                cantGoMore = true;
            }
        }
        return true;
    }

Log in to reply
 

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