Skip to content

Commit de3162c

Browse files
authored
Merge pull request #2155 from yuhyeon99/main
[yuhyeon99] WEEK 05 solutions
2 parents b6b287a + 8fd950f commit de3162c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
* i ๋ฒˆ์งธ ๋‚ ์— ์ฃผ์‹์˜ ๊ฐ€๊ฒฉ์„ prices[i]์— ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด prices๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,
5+
* ์ฃผ์‹์„ ์–ด๋–ค ๋‚ ์— ํ•œ ๋ฒˆ ์‚ฌ๊ณ  ๋‚˜์ค‘์— ๋‹ค๋ฅธ ๋‚ ์— ํŒ”์•„์„œ ๋‹ฌ์„ฑ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ด์ต์„ ๊ตฌํ•˜๋ผ.
6+
*
7+
*/
8+
var maxProfit = function(prices) {
9+
var maxProfit = 0;
10+
var minPrice = prices[0]
11+
12+
for(let i = 0; i < prices.length; i ++) {
13+
let profit = prices[i] - minPrice;
14+
maxProfit = Math.max(profit, maxProfit);
15+
minPrice = Math.min(prices[i], minPrice);
16+
}
17+
18+
return maxProfit;
19+
};

0 commit comments

Comments
ย (0)