File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
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
You canโt perform that action at this time.
0 commit comments