665. Non-decreasing Array
https://leetcode.com/problems/non-decreasing-array/description/
Input: nums = [4,2,3]
Output: true
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.Input: nums = [4,2,1]
Output: false
Explanation: You can't get a non-decreasing array by modify at most one element.Thoughts
Code
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
found = False
for i in range(0, len(nums) - 1):
if nums[i] <= nums[i + 1]: continue
if found: return False
found = True
if i > 0 and nums[i - 1] > nums[i + 1]: nums[i + 1] = nums[i]
return True
Analysis
Ver.2
Last updated
