Advertisement
yuntai

Untitled

May 25th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. class Solution:
  2.     def checkPossibility(self, nums: List[int]) -> bool:
  3.         cnt = 0
  4.         if len(nums) <= 1: return True
  5.         for i in range(1, len(nums)):
  6.             if nums[i-1] > nums[i]:
  7.                 if cnt > 0 or 1 < i < len(nums)-1 and nums[i-2] > nums[i] and nums[i+1] < nums[i-1]:
  8.                     return False
  9.                 cnt += 1
  10.         return True                
  11.    
  12.  
  13. #  -2  -1  i  +1
  14. #   7  10  5         -> can't decrease i-1     nums[i-2] > nums[i]
  15. #   7  10  5   3     -> can't increase i+1     nums[i+1] < nums[i-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement