python – How to rewrite this code from O(n**2) with n>1 to O(n)?

I would like to apologize in advance for this question because this is a very specific case, but I don’t know how else I can come to a solution.

The problem is as follows:

I have written a function that takes as input a list of 5 numbers, then the program’s goal is to return the largest drop in the list. It is important to understand that (5, 3, …) is a decrease of 2 and that (5, 20, …..) is a decrease of 0. In short, a decrease can only be from left to right and the largest decrease can for example also be the decrease from the 1st number to the 4th number, they do not necessarily have to be next to each other.

``````def biggest_drop(temperature):
difference = 0
new_temperature = temperature.copy()
a = 0
for i in temperature:
del new_temperature(0)
for j in new_temperature:
if i <= j:
False
else:
a = i - j
if a > difference:
difference = a
return difference
``````

The good news is the code works, the bad news is that because of the double for loop there is already a Big-O notation of O(n2). However, the program must have a Big-O notation of O(n1) and I don’t know how to fix this and where the speed of the code is also high enough. For example, here I have made a fairly sloppy alternative, but it is too slow:

``````def biggest_drop(temperature):
difference = 0
a = 0
b = temperature(1)
c = temperature(2)
d = temperature(3)
e = temperature(4)
for i in temperature:
if temperature(0) <= temperature(1) <= temperature(2) <= temperature(3) <= temperature(4):
return 0
elif i > b:
a = i - b
if a > difference:
difference = a
elif i > c:
a = i - c
if a > difference:
difference = a
elif i > d:
a = i - d
if a > difference:
difference = a
elif i > e:
a = i - e
if a > difference:
difference = a
return difference
``````

Do you have a better idea how my code complies with the big-O notation of O(n1) and is also fast enough for larger n, in other words lists with more than 5 integers?