[JAVA] Consider two case / T : O(N), S:O(1)


  • 0
    J
    class Solution {
        public boolean checkPossibility(int[] nums) {
            int count = 0;
            for(int i = 0; i < nums.length-1; i++){
                if(nums[i] > nums[i+1]){
                    // consider i+2
                    if(i+2 >= nums.length || (nums[i+2] >= nums[i])){
                        nums[i+1] = nums[i];
                    }
                    else if(i-1 < 0 || (nums[i+1] >= nums[i-1])){
                        nums[i] = nums[i+1];
                    }
                    else
                        return false;
                    count++;
                }
            }
            
            return count <= 1;
        }
    }
    

Log in to reply
 

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