File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ ๏ปฟ #ํด์
2+ #prices list๋ฅผ ์ํํ๋ฉด์ ์ต์๊ฐ min_val์ ์
๋ฐ์ดํธ ํ๋ค
3+ #ํ์ฌ prices[n]๊ณผ min_val์ ์ฐจ๋ฅผ ์
๋ฐ์ดํธ ํด์ค๋ค.
4+
5+
6+ #Big O
7+ #N: prices ์ ํฌ๊ธฐ
8+
9+ #Time Complexity: O(N)
10+ #- for loop : prices์ ์์ ๊ฐฏ์๋งํผ ์ํํ๋ฏ๋ก O(N)
11+
12+
13+ #Space Complexity: O(1)
14+ #- min_val, answer : ๋ณ์๋ ์์์ด๋ฏ๋ก O(1)
15+ class Solution (object ):
16+ def maxProfit (self , prices ):
17+
18+ #Initialize variables
19+ min_val = prices [0 ]
20+ answer = 0
21+
22+ for n in range (len (prices )):
23+ min_val = min (min_val ,prices [n ]) #Update min value of the prices list
24+ answer = max (prices [n ]- min_val ,answer ) #Update max value of prices[n] - min value
25+
26+ return answer
27+
You canโt perform that action at this time.
0 commit comments