Skip to content

Commit 6ecfde0

Browse files
feat: best-time-to-buy 풀이
1 parent 27bc6b3 commit 6ecfde0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var maxProfit = function (prices) {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (let price of prices) {
6+
// 가장 싼 가격을 갱신
7+
if (price < minPrice) {
8+
minPrice = price;
9+
} else {
10+
// 최대 이익을 갱신
11+
maxProfit = Math.max(maxProfit, price - minPrice);
12+
}
13+
}
14+
15+
return maxProfit;
16+
};

0 commit comments

Comments
 (0)