File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxProfit (self , prices : List [int ]) -> int :
3+ # profit: μ΅λ μ΄μ΅
4+ profit = 0
5+
6+ # current_minimum: νμ¬κΉμ§μ μ΅μ κ°κ²©
7+ current_minimum = prices [0 ]
8+
9+ # current_profit: νμ¬κΉμ§μ μ΅λ μ΄μ΅
10+ current_profit = 0
11+
12+ # μ 체 λ°°μ΄μμ μ΅μκ°κ³Ό, μ΅μκ° μΈλ±μ€ μ΄ν μ΅λκ°μ μ΄μ΅μΌλ‘ κ³μ°νλ λ°λ³΅λ¬Έ
13+ for i in range (1 , len (prices )):
14+ # μ΄μ΅μ΄ λ°μν κ²½μ° νμ¬ μ΄μ΅ κ°±μ
15+ # νμ¬ μ΄μ΅μ κ°±μ ν λλ§λ€ μ§κΈκΉμ§μ μ΅λ μ΄μ΅λ κ°±μ
16+ if prices [i ] > current_minimum :
17+ current_profit = max (current_profit , prices [i ] - current_minimum )
18+ profit = max (profit , current_profit )
19+ # μ΄μ΅μ΄ λ°μνμ§ μμ κ²½μ° μ΅μκ° κ°±μ
20+ else :
21+ current_minimum = prices [i ]
22+
23+ # μ΅λ μ΄μ΅λ§ μ μ₯λμ΄ λ°ν
24+ return profit
You canβt perform that action at this time.
0 commit comments