Skip to content

Commit f427957

Browse files
Samofalov AleksandrSamofalov Aleksandr
authored andcommitted
new task solved
1 parent 7ae1bbd commit f427957

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

InterviewAlgorithms/SellStock.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# Example 1:
3+
#
4+
# Input: prices = [7,1,5,3,6,4]
5+
# Output: 5
6+
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
7+
# Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
8+
9+
from typing import List
10+
11+
def best_sell(prices: List[float]) -> float:
12+
""" Return the maxi profit which you can take from this transcation otherwise 0"""
13+
min_price = float('inf')
14+
profit = 0
15+
for price in prices:
16+
if price < min_price:
17+
min_price = price
18+
profit = max(profit, price - min_price)
19+
return profit
20+
21+
if __name__ == '__main__':
22+
prices = [7, 1, 5, 3, 6, 4]
23+
print(best_sell(prices))

0 commit comments

Comments
 (0)