File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ฃผ์ ๊ฐ๊ฒฉ์ด ์ฃผ์ด์ง๋ prices ๋ฐฐ์ด์ด ์์ ๋ ์ต๋ ์ฃผ์ ์ด์ต์ ๊ตฌํ์์ค
3+ * ์ฃผ์์ ์ฐ ๋ ์ง์๋ ํ ์ ์์ผ๋ฉฐ ๋ฐ๋์ ์ฐ ๋ ์ง์ ์ดํ ๋ ์ง๋ถํฐ(๋ฏธ๋๋ถํฐ) ํ ์ ์๋ค.
4+ */
5+ class Solution {
6+ public int maxProfit (int [] prices ) {
7+ int maxProfit = 0 ;
8+ int min = prices [0 ];
9+ // ๊ตณ์ด DP ๋ฐฐ์ด ์ฐ์ง ์๊ณ ๊ณ์ฐ, ๊ณต๊ฐ ๋ณต์ก๋ ๋ฎ์ถ๊ธฐ
10+ for (int i = 0 ; i < prices .length ; i ++) {
11+ int profit = prices [i ] - min ;
12+ maxProfit = Math .max (profit , maxProfit );
13+ min = Math .min (prices [i ], min );
14+ }
15+ return maxProfit ;
16+ }
17+
18+ // public int maxProfit(int[] prices) {
19+ // // ์ต์ ๊ตฌ๋งค
20+ // int[] dp = new int[prices.length];
21+ // dp[0] = prices[0];
22+ // for (int i = 1; i < prices.length; i++) {
23+ // dp[i] = Math.min(prices[i], dp[i - 1]);
24+ // }
25+ // // ์ต์ ๊ตฌ๋งค ๋ฐฐ์ด ๊ธฐ์ค์ผ๋ก ๋น์ผ ์ต๋ ์ด์ต ๊ณ์ฐ
26+ // int profit = 0;
27+ // for (int i = 1; i < prices.length; i++) {
28+ // profit = Math.max(prices[i] - dp[i - 1], profit);
29+ // }
30+ // return profit;
31+ // }
32+ }
33+
You canโt perform that action at this time.
0 commit comments