My JAVA solution. No input modifying, no greedy


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

Log in to reply
 

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