Skip to content

Commit ae60758

Browse files
committed
week 5 solution
1 parent e191914 commit ae60758

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(1)
4+
5+
Dynamic programming
6+
"""
7+
8+
class Solution:
9+
def maxProfit(self, prices: List[int]) -> int:
10+
max_profit = 0
11+
min_price = prices[0]
12+
for p in prices:
13+
max_profit = max(max_profit, p - min_price)
14+
min_price = min(min_price, p)
15+
16+
return max_profit

0 commit comments

Comments
 (0)