Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. class Solution {
  2. public boolean checkPossibility(int[] nums) {
  3. int changes = 0;
  4.  
  5. if (nums.length < 3) {
  6. return true;
  7. }
  8.  
  9. if (nums[0] > nums[1]) {
  10. if (nums[1] > nums[2]) {
  11. return false;
  12. }
  13. changes++;
  14. }
  15.  
  16. for (int i = 1; i < nums.length - 2; i++) {
  17. if (nums[i] > nums[i + 1]) {
  18. if (changes > 0) {
  19. return false;
  20. }
  21.  
  22. if (i < nums.length - 2) {
  23. if (nums[i + 1] > nums[i + 2]) {
  24. return false;
  25. }
  26. if (nums[i] > nums[i + 2]) {
  27. if (i > 0 && nums[i - 1] > nums[i + 1]) {
  28. return false;
  29. }
  30. }
  31. changes++;
  32. i++;
  33. }
  34. }
  35. }
  36.  
  37. if (nums[nums.length - 2] > nums[nums.length - 1]) {
  38. if (changes > 0) {
  39. return false;
  40. }
  41. }
  42.  
  43. return true;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement