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
+ class SolutionJaeJeong {
2
+
3
+ public int maxProfit (int [] prices ) {
4
+ // ์ฃผ์ด์ง ๊ฐ๊ฒฉ ๋ฐฐ์ด์ด ์ฃผ์ด์ง๊ณ prices[i]๋ i๋ฒ์งธ ๋ ์ ์ฃผ์ด์ง ์ฃผ์ ๊ฐ๊ฒฉ
5
+ // ํ ์ฃผ์์ ๋งค์ํ ๋จ์ผ ๋ ์ง๋ฅผ ์ ํํ๊ณ , ๋งค๋ํ ๋ฏธ๋์ ๋ค๋ฅธ ๋ ์ง๋ฅผ ์ ํํด ์์ต์ ๊ทน๋ํ
6
+ // ์ด ๊ฑฐ๋์์ ์ป์ ์ ์๋ ์ต๋ ์์ต์ ๋ฐํํด๋ผ, ์์ต์ ์ป์ ์ ์์ผ๋ฉด 0์ ๋ฐํํด๋ผ
7
+ // ์ธ๊ฒ ๋งค์ํด์ ๋น์ธ๊ฒ ๋งค๋ํด๋ผ
8
+
9
+ // 3๋ฒ์งธ ํ์ด
10
+ // ์ต๋ ์ด์ต: ํ์ฌ ๊ฐ๊ฒฉ - ์ด์ ๊ฐ๊ฒฉ ์ค ์ต์ ๊ฐ๊ฒฉ
11
+ // ์๊ฐ๋ณต์ก๋: O(N), ๊ณต๊ฐ๋ณต์ก๋: O(1)
12
+ int minPrice = prices [0 ];
13
+ int maxProfit = 0 ;
14
+ for (int i = 1 ; i < prices .length ; i ++) {
15
+ if (prices [i - 1 ] < minPrice ) {
16
+ minPrice = prices [i - 1 ];
17
+ }
18
+
19
+ var profit = prices [i ] - minPrice ;
20
+ if (profit > maxProfit ) {
21
+ maxProfit = profit ;
22
+ }
23
+ }
24
+
25
+ return maxProfit ;
26
+ }
27
+ }
You canโt perform that action at this time.
0 commit comments