Input min_price None max_price None max_profit for price in enumerate

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Input = [5,9,8,7,6,5,4,3,2,3]
min_price = None
max_price = None
max_profit = 0
for i, price in enumerate(Input):
if i+1!=len(Input):
if min_price is None and price < Input[i+1]:
min_price = price
max_price = Input[i+1]
max_profit = max(max_profit, Input[i + 1] - price)
elif min_price is None and price > Input[i+1]:
min_price = Input[i+1]
max_price = Input[i+1]
elif price < min_price:
min_price = price
max_profit = max(max_profit, Input[i+1] - min_price)
elif Input[i+1] > max_price:
max_price = Input[i+1]
max_profit = max(max_profit, Input[i + 1] - min_price)