Skip to content

Commit a01fdcf

Browse files
committed
update Best Time to Buy and Sell Stock solution
1 parent a48ed05 commit a01fdcf

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

best-time-to-buy-and-sell-stock/clara-shin.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@ var maxProfit = function (prices) {
1616

1717
// 두 번째 날부터
1818
for (let i = 1; i < prices.length; i++) {
19-
// 현재 가격이 최저 가격보다 낮으면 최저 가격 업데이트
20-
if (prices[i] < minPrice) {
21-
minPrice = prices[i];
22-
}
23-
// 현재 가능한 이익 계산 (현재 가격 - 최저 가격)
24-
const currentProfit = prices[i] - minPrice;
25-
26-
// 최대 이익 업데이트
27-
if (currentProfit > maxProfit) {
28-
maxProfit = currentProfit;
29-
}
19+
minPrice = Math.min(minPrice, prices[i]); // 최저 가격 갱신
20+
maxProfit = Math.max(maxProfit, prices[i] - minPrice); // 최대 이익 갱신
3021
}
3122

3223
return maxProfit;

0 commit comments

Comments
 (0)