package leetcode;

/**
 * https://leetcode.com/problems/non-decreasing-array/
 */
public class Problem665 {
    public boolean checkPossibility(int[] nums) {
        if (nums.length == 1) {
            return true;
        }
        int prev = 0;
        int count = 0;
        for (int curr = 1; curr < nums.length; curr++) {
            if (nums[prev] > nums[curr]) {
                if (prev == 0) {
                    nums[prev] = Integer.MIN_VALUE;
                } else {
                    if (nums[prev] == nums[prev - 1]) {
                        nums[curr] = nums[prev];
                    } else {
                        nums[prev] = nums[prev - 1];
                    }
                    if (nums[prev] > nums[curr]) {
                        return false;
                    }
                }
                count++;
                if (count == 2) {
                    return false;
                }
            }
            prev = curr;
        }
        return true;
    }

    public static void main(String[] args) {
        Problem665 prob = new Problem665();
        System.out.println(prob.checkPossibility(new int[]{4, 2, 3})); // true
        System.out.println(prob.checkPossibility(new int[]{4, 2, 1})); // false
        System.out.println(prob.checkPossibility(new int[]{1, 5, 2, 4})); // true
        System.out.println(prob.checkPossibility(new int[]{1})); // true
        System.out.println(prob.checkPossibility(new int[]{3, 4, 2, 3})); // false
        System.out.println(prob.checkPossibility(new int[]{-1, 4, 2, 3})); // true
        System.out.println(prob.checkPossibility(new int[]{2, 3, 3, 2, 4})); // true
        System.out.println(prob.checkPossibility(new int[]{1, 2, 4, 5, 3})); // true
    }
}

@@ -433,25 +433,18 @@
 %5D = 
-Integer.MIN_VALUE
+nums%5Bcurr%5D
 ;%0A  
@@ -503,26 +503,26 @@
 prev
+ - 1
 %5D 
-=
+%3E
 = nums%5B
-prev - 1
+curr
 %5D) %7B
@@ -646,128 +646,13 @@
 ums%5B
-prev - 1%5D;%0A                    %7D%0A                    if (nums%5Bprev%5D %3E nums%5Bcurr%5D) %7B%0A                        return false
+curr%5D
 ;%0A  
@@ -841,32 +841,32 @@
 curr;%0A        %7D%0A
+
         return t
@@ -879,753 +879,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem665 prob = new Problem665();%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B4, 2, 3%7D)); // true%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B4, 2, 1%7D)); // false%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B1, 5, 2, 4%7D)); // true%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B1%7D)); // true%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B3, 4, 2, 3%7D)); // false%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B-1, 4, 2, 3%7D)); // true%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B2, 3, 3, 2, 4%7D)); // true%0A        System.out.println(prob.checkPossibility(new int%5B%5D%7B1, 2, 4, 5, 3%7D)); // true%0A    %7D
 %0A%7D%0A

Solve problem 665