Created
May 22, 2018 03:56
-
-
Save Siyu-Lei/ec8cdb5d2cd01ca81558b3e6d033793f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean checkPossibility(int[] nums) { | |
int count = 0; | |
for (int i = 1; i < nums.length; i++) { | |
if (nums[i - 1] > nums[i]) { | |
count++; | |
if (count > 1) { | |
return false; | |
} | |
if (i < 2 || nums[i - 2] <= nums[i]) { | |
//if decrease nums[i - 1] is safe, then decrease it is always a good choice | |
//nums[i] >= nums[i - 2] makes decrease nums[i - 1] a safe choice | |
nums[i - 1] = nums[i]; | |
} else { | |
nums[i] = nums[i - 1]; | |
} | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment