Skip to content

Commit 23ea823

Browse files
authored
Create Non-decreasing Array.py
1 parent 9275fb5 commit 23ea823

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
3+
Problem 665 | Non-decreasing Array
4+
https://leetcode.com/problems/non-decreasing-array/
5+
6+
'''
7+
8+
class Solution:
9+
def checkPossibility(self, nums: List[int]) -> bool:
10+
changed = False
11+
for i in range(len(nums)-1):
12+
if nums[i] <= nums[i+1]:
13+
continue
14+
if changed:
15+
return False
16+
if i == 0 or nums[i+1] >= nums[i-1]:
17+
nums[i] = nums[i+1]
18+
else:
19+
nums[i+1] = nums[i]
20+
changed = True
21+
return True

0 commit comments

Comments
 (0)