LeetCode 솔루션 분류
[6/25] 665. Non-decreasing Array
본문
Medium
3709670Add to ListShareGiven an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one 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:
Input: nums = [4,2,3] Output: true Explanation: You could modify the first4to1to get a non-decreasing array.
Example 2:
Input: nums = [4,2,1] Output: false Explanation: You can't get a non-decreasing array by modify at most one element.
Constraints:
n == nums.length1 <= n <= 104-105 <= nums[i] <= 105
관련자료
-
링크
댓글 1
학부유학생님의 댓글
- 익명
- 작성일
Runtime: 193 ms, faster than 92.63% of Python3 online submissions for Non-decreasing Array.
Memory Usage: 15.4 MB, less than 5.92% of Python3 online submissions for Non-decreasing Array.
Memory Usage: 15.4 MB, less than 5.92% of Python3 online submissions for Non-decreasing Array.
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
modified = False
nums = [-10e5] + nums + [10e5]
for i in range(len(nums[1:])):
if nums[i] <= nums[i+1]: continue
if modified: return False
if nums[i-1]<=nums[i+1]: nums[i] = nums[i+1]
else: nums[i+1] = nums[i]
modified = True
return True







