From bde1faf984a6b98d53ea87a0b1d124c06d95f830 Mon Sep 17 00:00:00 2001 From: Robin Yoon Date: Mon, 8 Dec 2025 14:30:43 +0900 Subject: [PATCH 1/2] best time to buy and sell stock --- .../robinyoon-dev.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/robinyoon-dev.js diff --git a/best-time-to-buy-and-sell-stock/robinyoon-dev.js b/best-time-to-buy-and-sell-stock/robinyoon-dev.js new file mode 100644 index 0000000000..b57408abb2 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/robinyoon-dev.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + + // NOTE: 해설 보고 쓴 코드입니다. + // i 번째 날에 prices[i]의 가격으로 주식을 팔아서 가장 큰 이익을 내려면 주식을 언제 샀어야 했을까? + // 정답은 바로 i 번째 날이 오기 전에 주식이 가장 쌌던 날 입니다! + + let maxProfit = 0; + let minPrice = prices[0]; + + for(const price of prices){ + const profit = price - minPrice + maxProfit = Math.max(maxProfit, profit); + minPrice = Math.min(price, minPrice); + } + + return maxProfit; + +}; \ No newline at end of file From 891ad63c2870a093c101f518123d161a8170d2f0 Mon Sep 17 00:00:00 2001 From: Robin Yoon Date: Mon, 8 Dec 2025 14:34:22 +0900 Subject: [PATCH 2/2] fix lint error --- best-time-to-buy-and-sell-stock/robinyoon-dev.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/best-time-to-buy-and-sell-stock/robinyoon-dev.js b/best-time-to-buy-and-sell-stock/robinyoon-dev.js index b57408abb2..d7dcb423e0 100644 --- a/best-time-to-buy-and-sell-stock/robinyoon-dev.js +++ b/best-time-to-buy-and-sell-stock/robinyoon-dev.js @@ -19,4 +19,4 @@ var maxProfit = function(prices) { return maxProfit; -}; \ No newline at end of file +};