665. Non-decreasing Array
https://leetcode.com/problems/non-decreasing-array/
Python
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
if len(nums) <= 2:
return True
is_modified = False
for i in range(1, len(nums)):
left, right = nums[i-1], nums[i]
if left <= right:
continue
if is_modified:
return False
is_modified = True
if i < 2 or nums[i-2] <= nums[i]:
nums[i-1] = nums[i]
else:
nums[i] = nums[i-1]
return True