-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
665_Non-decreasing_Array.py
38 lines (37 loc) · 1.32 KB
/
665_Non-decreasing_Array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution(object):
# def checkPossibility(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# pos = None
# # Check if there are more than 2 broken point
# # record first index
# for i in range(len(nums) - 1):
# if nums[i] > nums[i + 1]:
# if pos is not None:
# return False
# pos = i
# if pos is None or pos == 0 or pos == len(nums) - 2:
# return True
# # Remove pos or remove pos + 1
# return (nums[pos - 1] <= nums[pos + 1] or nums[pos] <= nums[pos + 2])
def checkPossibility(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# https://leetcode.com/problems/non-decreasing-array/discuss/106826/JavaC%2B%2B-Simple-greedy-like-solution-with-explanation
broken_num = 0
for i in range(len(nums) - 1):
if (nums[i] > nums[i + 1]):
broken_num += 1
if broken_num >= 2:
return False
if (i - 1 < 0 or nums[i - 1] <= nums[i + 1]):
# Remove i
nums[i] = nums[i + 1]
else:
# Remove i + 1
nums[i + 1] = nums[i]
return True