Skip to content

Commit cdf5155

Browse files
authored
Merge pull request keon#75 from dong-jy/patch-2
Update buy_sell_stock.py
2 parents ebfe8fe + ef4db28 commit cdf5155

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

dp/buy_sell_stock.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,24 @@
2020
"""
2121

2222

23-
def max_profit(prices):
23+
# O(n^2) time
24+
def max_profit_naive(prices):
25+
"""
26+
:type prices: List[int]
27+
:rtype: int
28+
"""
29+
max_so_far = 0
30+
for i in range(0, len(prices) - 1):
31+
for j in range(i + 1, len(prices)):
32+
max_so_far = max(max_so_far, prices[j] - prices[i])
33+
return max_so_far
34+
35+
36+
# O(n) time
37+
def max_profit_optimized(prices):
2438
"""
2539
input: [7, 1, 5, 3, 6, 4]
26-
diff : [X,-6, 5,-2, 3,-2]
40+
diff : [X, -6, 4, -2, 3, -2]
2741
:type prices: List[int]
2842
:rtype: int
2943
"""

0 commit comments

Comments
 (0)