-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnon.py
More file actions
161 lines (142 loc) · 4.21 KB
/
non.py
File metadata and controls
161 lines (142 loc) · 4.21 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Pending...
class Solution(object):
def checkPossibility(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# 100 pass logic
n = len(nums)
# Length is One Handle
if n == 1:
return True
# Iterate to the point of first failure of a nondecreasing array
for i in range(0,n-1):
# failure of non-decreasing sceanrio
if nums[i] > nums[i+1]:
# At the point of such an failure, consider both the elements
# With removing first element i
select = nums[:i]+nums[i+1:]
# With removing the next element i+1
select1 = nums[:i+1]+nums[i+2:]
# Compare the sorted array with selected
if select == sorted(select):
return True
elif select1 == sorted(select1):
return True
else:
return False
return True
"""
count = 0
n = len(nums)
for i in range(1,n-1):
if nums[i] > nums[i+1] and nums[i-1] < nums[i+1]:
count += 1
if count > 1:
return False
else:
return True
"""
"""
# Working half the way
n = len(nums)
count = 0
for i in range(n):
if i == n-1:
if nums[i] >= nums[i-1] and max(nums) == nums[i]:
count += 1
elif nums[i] <= nums[i+1]:
count += 1
if count == n-1:
return True
else:
return False
"""
"""
# Check for a not condition rather than a positive condition like above
n = len(nums)
count = 0
for i in range(n-1):
if nums[i]
if nums[i] > nums[i+1]:
count += 1
if count > 1:
return False
else:
return True
"""
"""
# compare between sorted pair and the original array
original = nums
sort_original = sorted(nums)
print original, sort_original
n = len(original)
count = 0
i = j = 0
while i < n:
if original[i] != sort_original[j]:
count += 1
i += 1
else:
i += 1
j += 1
if count > 1:
return False
else:
return True
"""
"""
# Try to make it non decreasing array
n = len(nums)
select = None
c = 0
for i in range(n-1):
if nums[i] < nums[i+1]:
pass
else:
if select is None:
select = i
else:
nums[select],nums[i] = nums[i], nums[select]
select = None
c += 1
if select:
nums.append(select)
c += 1
if nums[n-1] != nums[n-2] and nums[n-1] in nums[:n-1]:
c += 1
if c > 1:
return False
else:
return True
"""
"""
n = len(nums)
count = 0
for i in range(n):
#print i
if i < n-1 and nums[i] > nums[i+1]:
count += 1
if count > 1:
break
if i > 0 and nums[i] != nums[i-1] and nums[i] in nums[:i]:
count += 1
if count > 1:
break
if count > 1:
return False
else:
return True
"""
# Swap and check else fail
# OR if such a over rule occurs, try to delete the element and check if it is perfectly sorted
n = len(nums)
for i in range(n-1):
if nums[i] > nums[i+1]:
nums = nums[:i]+nums[i+1:]
if nums == sorted(nums):
return True
else:
return False
return True