-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.- almostIncreasingSequence.py
41 lines (32 loc) · 1.13 KB
/
7.- almostIncreasingSequence.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
39
40
41
# pylint: disable=too-few-public-methods
# pylint: disable=invalid-name
"""
Given a sequence of integers as an array, determine whether it is possible to obtain
a strictly increasing sequence by removing no more than one element from the array.
Example
For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to
get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2].
Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].
"""
# Code
def almostIncreasingSequence(sequence):
droppped = False
last = prev = min(sequence) - 1
for elm in sequence:
if elm <= last:
if droppped:
return False
else:
droppped = True
if elm <= prev:
prev = last
elif elm >= prev:
prev = last = elm
else:
prev, last = last, elm
return True