File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def maxProfit (self , prices ):
3+ """
4+ :type prices: List[int]
5+ :rtype: int
6+ ์ฃผ์ด์ง prices๋ฅผ ํ ๋ฒ๋ง ์ํํ๋ฉด์
7+ ์ง๊ธ๊น์ง์ ์ต์๊ฐ์ ์ถ์ ํ๊ณ ,
8+ 'ํ์ฌ ๊ฐ๊ฒฉ - ์ง๊ธ๊น์ง์ ์ต์๊ฐ'์ผ๋ก ๊ณ์ฐ๋๋ ์ด์ต์ด
9+ '์ง๊ธ๊น์ง์ ์ต๋ ์ด์ต(์ด๊ธฐ 0)๋ณด๋ค ํฌ๋ฉด ๊ฐฑ์ ํ์ฌ
10+ ์ต์ข
์ต๋ ์ด์ต ๊ตฌํ๊ธฐ ๋ฌธ์
11+ Time Complexity: O(n)
12+ Space Complexity: O(1)
13+ """
14+ max_profit = 0 # ์ด์ต์ด ์์ ๋ 0์ ๋ฐํํ๊ฒ ์ด๊ธฐํ
15+ min_price = float ('inf' ) # ์ต์ ๊ฐ๊ฒฉ ์ ์ฅ, ๋ฌดํ๋๋ก ์ด๊ธฐํํด์ ๋ฃจํ ์ฒซ ๋ฒ์งธ ๊ฐ๊ฒฉ๋ถํฐ ์ต์ ๊ฐ๊ฒฉ์ ์ ์ฅ
16+
17+ for price in prices :
18+ # ์ต๋ ์ด์ต ๊ฐฑ์
19+ max_profit = max (max_profit , (price - min_price ))
20+ # ์ต์ ๊ฐ๊ฒฉ ๊ฐฑ์
21+ min_price = min (min_price , price )
22+ # ์ต์ ์ด์ต ๊ฐฑ์ ์ดํ ์ต์ ๊ฐ๊ฒฉ ๊ฐฑ์ ํด์ผ ํจ
23+ # ์ต๋ ์ด์ต ์์ฒด๋ ์ด๋ฏธ '์ฐ' ์ฃผ์์ ๋ํด ๊ณ์ฐํด์ผ ํ๋ฏ๋ก
24+ # ์ฌ๋ ๋์ ํ ์ ์์
25+
26+ return max_profit
You canโt perform that action at this time.
0 commit comments