File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ 1. 1 <= prices.length <= 10^5
4+ 2. 0 <= prices[i] <= 10^4
5+
6+ Time Complexity: O(n)
7+
8+ Space Complexity: O(1)
9+
10+ ํ์ด ๋ฐฉ๋ฒ:
11+ - ๋ฐฐ์ด์ ํ ๋ฒ ์ํํ๋ฉด์:
12+ 1. ํ์ฌ๊น์ง์ ์ต์ ๊ฐ๊ฒฉ(min_price)์ ๊ณ์ ๊ฐฑ์
13+ 2. ํ์ฌ ๊ฐ๊ฒฉ์์ ์ต์ ๊ฐ๊ฒฉ์ ๋บ ๊ฐ(ํ์ฌ ๊ฐ๋ฅํ ์ด์ต)๊ณผ ๊ธฐ์กด ์ต๋ ์ด์ต์ ๋น๊ตํ์ฌ ๋ ํฐ ๊ฐ์ ์ ์ฅ
14+ - ์ด ๋ฐฉ์์ผ๋ก ๊ฐ ์์ ์์ ๊ฐ๋ฅํ ์ต๋ ์ด์ต์ ๊ณ์ฐํจ
15+
16+ To Do:
17+ - ๋ค๋ฅธ ์ ๊ทผ ๋ฐฉ๋ฒ ์ฐพ์๋ณด๊ธฐ (Two Pointers, Dynamic Programming)
18+ """
19+ class Solution :
20+ def maxProfit (self , prices : List [int ]) -> int :
21+ min_price = prices [0 ]
22+ max_profit = 0
23+
24+ for i in range (1 , len (prices )):
25+ min_price = min (min_price , prices [i ])
26+ max_profit = max (max_profit , prices [i ] - min_price )
27+
28+ return max_profit
129
You canโt perform that action at this time.
0 commit comments