We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a48ed05 commit a01fdcfCopy full SHA for a01fdcf
best-time-to-buy-and-sell-stock/clara-shin.js
@@ -16,17 +16,8 @@ var maxProfit = function (prices) {
16
17
// 두 번째 날부터
18
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
+ minPrice = Math.min(minPrice, prices[i]); // 최저 가격 갱신
+ maxProfit = Math.max(maxProfit, prices[i] - minPrice); // 최대 이익 갱신
30
}
31
32
return maxProfit;
0 commit comments