Skip to content

Commit b6b287a

Browse files
authored
Merge pull request #2154 from robinyoon-dev/main
[robinyoon-dev] WEEK 05 solutions
2 parents 5f06e53 + 891ad63 commit b6b287a

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
ย (0)