Skip to content

Commit dedc040

Browse files
committed
best-time-to-buy-and-sell-stock solution
1 parent abcb7d3 commit dedc040

File tree

1 file changed

+14
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)