Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Add Comment
Please, Sign In to add comment