Skip to content

Commit fc48533

Browse files
committed
feat: Solve best-time-to-buy-and-sell-stock problem
1 parent ca7f3b9 commit fc48533

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
1. ๋ธŒ๋ฃจํŠธํฌ์Šค
4+
2์ค‘ for๋ฌธ์ด๋ผ์„œ O(n^2)์ž„.
5+
prices์˜ ๊ธธ์ด๊ฐ€ 10^5์ด๋ฏ€๋กœ 10^10์ด ๋˜๋ฉด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒ
6+
7+
2. ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ๊ฐ€๋Šฅํ• ๊นŒ ํ–ˆ์ง€๋งŒ DP๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ ๊ฐ™์Œ
8+
์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด ๋‚˜์˜ด
9+
"""
10+
"""
11+
def maxProfit(self, prices: List[int]) -> int:
12+
max_profit = 0
13+
for i in range(len(prices)-1):
14+
for j in range(i, len(prices)):
15+
profit = prices[j] - prices[i]
16+
max_profit = max(max_profit, profit)
17+
return max_profit
18+
"""
19+
def maxProfit(self, prices: List[int]) -> int:
20+
max_profit = 0
21+
min_price = prices[0]
22+
for price in prices:
23+
max_profit = max(max_profit, price - min_price)
24+
min_price = min(price, min_price)
25+
return max_profit

0 commit comments

Comments
ย (0)