Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
Example
Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Solution
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
count = 0
if len(nums)==1:
return True
for i in range(len(nums)-1):
if nums[i]>nums[i+1]:
count += 1
if i == 0:
nums[i] = nums[i+1]
elif nums[i-1]<=nums[i+1]:
nums[i] = nums[i-1]
else:
nums[i+1] = nums[i]
if count>1:
return False
return True