From 2ed89c442b4f982883294ffd9f19a6130e218abf Mon Sep 17 00:00:00 2001 From: smosco Date: Thu, 11 Dec 2025 11:34:09 +0900 Subject: [PATCH] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/smosco.js | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/smosco.js diff --git a/best-time-to-buy-and-sell-stock/smosco.js b/best-time-to-buy-and-sell-stock/smosco.js new file mode 100644 index 000000000..06e21ae6e --- /dev/null +++ b/best-time-to-buy-and-sell-stock/smosco.js @@ -0,0 +1,25 @@ +/** + * 핵심 아이디어: + * 어떤 날에 주식을 팔 때 최대 이익을 얻으려면, + * 그 날 이전의 최저가에 샀어야 한다. + * 따라서 배열을 순회하면서 "지금까지의 최저가"만 추적하고, + * 매 시점마다 (현재가 - 최저가)로 잠재 이익을 계산하면 된다. + * + * 시간 복잡도: O(n) - 배열을 한 번만 순회 + * 공간 복잡도: O(1) - 변수 2개만 사용 + */ +const maxProfit = (prices) => { + let minPrice = prices[0]; + let maxProfit = 0; + + for (let i = 1; i < prices.length; i++) { + const price = prices[i]; + if (price < minPrice) { + minPrice = price; + } else if (price - minPrice > maxProfit) { + maxProfit = price - minPrice; + } + } + + return maxProfit; +};