Skip to content

Commit b406411

Browse files
authored
Create Best Time to Buy and Sell Stock - Leetcode 121.py
1 parent fc69a97 commit b406411

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
# Time: O(n)
4+
# Space: O(1)
5+
min_price = float('inf')
6+
max_profit = 0
7+
8+
for price in prices:
9+
if price < min_price:
10+
min_price = price
11+
12+
profit = price - min_price
13+
14+
if profit > max_profit:
15+
max_profit = profit
16+
17+
return max_profit

0 commit comments

Comments
 (0)