#Codefight almostIncreasingSequence
Chào các bác, em đang ngồi mò mẫm làm bài tập python trên codefight, và đang dừng ở bài này
almostIncreasingSequence
Mô tả yêu cầu của em nó:
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].
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.
[output] boolean
Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.
Tạm dịch (em mượn bản dịch của 1 bạn em google đc)
Tạm dịch đề bài: Cho một dãy số bất kì, kiểm tra xem nếu xóa 1 số thuộc dãy số đó thì ta có thu được 1 dãy tăng dần không. Yêu cầu các số trong dãy thu được sau khi xóa phải khác nhau
Đây là code của em:
def almostIncreasingSequence(sequence):
if 2<=len(sequence)<=10**5:
if any(-10**5<=sequence[i]<=10**5 for i in range(0,len(sequence)-1)):
if len(sequence) ==2:
return True
else:
count=0
# list(sequence)
# counta=0
for i in range(0,len(sequence)-1,1):
if sequence.count(sequence[i]) >1:
count=100
for i in range(0,len(sequence)-2,1):
if sequence[i+1]-sequence[i] != 1:
count+=1
print(count)
if count >1:
return False
else:
return True
Thuật toán em k biết sau đâu ko, nhưng chạy test thì toàn báo tạch :((( nhờ các cao nhân cho em ít ánh sáng, em cám ơn
Input:
sequence: [0, -2, 5, 6]
Output:
false
Expected Output:
true
Console Output:
2
Input:
sequence: [1, 2, 5, 3, 5]
Output:
false
Expected Output:
true
Console Output:
102