File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ๋ฌธ์ : ํ๋ฒ์ ๊ตฌ๋งค์ ํ๋งค๋ก ์ป์ ์ ์๋ ์ต๋ ์ด์ต์ ๊ตฌํ๋ผ.
3+ ํ์ด: ์ฃผ์ด์ง ๊ฐ๊ฒฉ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ์ต์ ๊ฐ์ ์ต๊ณ ๊ฐ๋ฅผ ๊ฐฑ์ ํ๋ฉฐ ์ต๋ ์ด์ต์ ๊ณ์ฐํ๋ค.
4+ ์ต์ ๊ฐ๋ ํ์ฌ ์ต์ ๊ฐ๊ฒฉ๋ณด๋ค ๋ฎ์ ๊ฐ๊ฒฉ์ด ๋์ค๋ฉด ๊ฐฑ์ ํ๊ณ ,
5+ ์ต๊ณ ๊ฐ๋ ํ์ฌ ์ต๊ณ ๊ฐ๊ฒฉ๋ณด๋ค ๋์ ๊ฐ๊ฒฉ์ด ๋์ค๋ฉด ๊ฐฑ์ ๋ฐ ์ ๋ต์ธ์ง ๋น๊ตํ๋ค.
6+ ์๊ฐ๋ณต์ก๋: O(n)
7+ ๊ณต๊ฐ๋ณต์ก๋: O(1)
8+
9+ '''
10+
11+
12+ class Solution :
13+ def maxProfit (self , prices : List [int ]) -> int :
14+ ma , mi = prices [0 ], prices [0 ]
15+ answ = 0
16+ for i in range (1 , len (prices )):
17+ if mi > prices [i ]:
18+ mi , ma = prices [i ], prices [i ]
19+ if ma < prices [i ]:
20+ ma = prices [i ]
21+ answ = max (answ , ma - mi )
22+ return answ
You canโt perform that action at this time.
0 commit comments