665. Non-decreasing Array
https://leetcode.com/problems/non-decreasing-array/description/
Last updated
Was this helpful?
https://leetcode.com/problems/non-decreasing-array/description/
Last updated
Was this helpful?
Given an array nums
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 nums[i] <= nums[i + 1]
holds for every i
(0-based) such that (0 <= i <= n - 2)
.
Example 1:
Example 2:
问给定数组在至多只改变一个数的条件下能否满足非严格单调递增。在出现不满足非严格递增nums[i]和nums[i+1]时,由于只能改一个,被改的元素只能从它俩中选。当nums[i + 1]比nums[i -1]还小时,只能通过修改nums[i+1]来满足『至多一个』的条件。改完后继续遍历,当再次出现不满足非严格递增时返回False。
时间复杂度O(N).
不修改原数组版本。