Guest User

Untitled

a guest
May 26th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class Solution {
  2. public boolean checkPossibility(int[] nums) {
  3. int count = 0;
  4. for (int i = 1; i < nums.length; i++) {
  5. if (nums[i - 1] > nums[i]) {
  6. count++;
  7. if (count > 1) {
  8. return false;
  9. }
  10. if (i < 2 || nums[i - 2] <= nums[i]) {
  11. //if decrease nums[i - 1] is safe, then decrease it is always a good choice
  12. //nums[i] >= nums[i - 2] makes decrease nums[i - 1] a safe choice
  13. nums[i - 1] = nums[i];
  14. } else {
  15. nums[i] = nums[i - 1];
  16. }
  17. }
  18. }
  19. return true;
  20. }
  21. }
Add Comment
Please, Sign In to add comment