Skip to content

Commit 2ae36ba

Browse files
committed
Week 5: best-time-to-buy-and-sell-stock
1 parent 6a14065 commit 2ae36ba

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
class Solution:
4+
def maxProfit(self, prices: List[int]) -> int:
5+
min_price = prices[0]
6+
max_price = prices[0]
7+
temp_min = prices[0]
8+
diff = 0
9+
for price in prices:
10+
temp_diff = price - temp_min
11+
if temp_diff < 0:
12+
temp_min = price
13+
elif temp_diff > diff:
14+
max_price = price
15+
min_price = temp_min
16+
diff = temp_diff
17+
else:
18+
continue
19+
return diff
20+

0 commit comments

Comments
 (0)