diff --git a/bangdori/Best Time to Buy and Sell Stock.js b/bangdori/Best Time to Buy and Sell Stock.js new file mode 100644 index 0000000..4332328 --- /dev/null +++ b/bangdori/Best Time to Buy and Sell Stock.js @@ -0,0 +1,18 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let buyPrice = Infinity; + let maxProfit = 0; + + for (const price of prices) { + if (buyPrice > price) { + buyPrice = price; + } else { + maxProfit = Math.max(maxProfit, price - buyPrice); + } + } + + return maxProfit; +};