Java solution with explanation


  • 3

    Given: a1, a2, a3, a4, a5, ...

    Suppose a4<a3, we must modify either a3 or a4, the goal is to make a1...a4 satisfy the conditions and a4 has a relative small value.

    if a4<a2, then we have to modify a4, and the best choice is to let a4 = a3;
    otherwise, it's better to modify a3 to let a3 = a4 (no change to a4);

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

  • 0
    P

    nice explanation..


Log in to reply
 

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