From 181a129f4555d03fc646ee87d5f0e4e861b3448f Mon Sep 17 00:00:00 2001 From: jeehay Date: Mon, 6 Jan 2025 21:09:22 +0900 Subject: [PATCH] Add best-time-to-buy-and-sell-stock solution --- best-time-to-buy-and-sell-stock/Jeehay28.js | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/Jeehay28.js diff --git a/best-time-to-buy-and-sell-stock/Jeehay28.js b/best-time-to-buy-and-sell-stock/Jeehay28.js new file mode 100644 index 000000000..1964dd1e5 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Jeehay28.js @@ -0,0 +1,41 @@ +/** + * @param {number[]} prices + * @return {number} + */ + +// TC : O(n) +// SC : O(1) + +var maxProfit = function (prices) { + if (prices.length === 1) { + return 0; + } + + // Two variables (profitMax and priceMin) are used to store the maximum profit and minimum price seen, which require O(1) space. + let profitMax = 0; + let priceMin = prices[0]; + + for (const price of prices) { + const profit = price - priceMin; + profitMax = Math.max(profit, profitMax); + priceMin = Math.min(price, priceMin); + } + + return profitMax; +}; + +// Why Constants Are Ignored in Big-O +// In Big-O notation, O(2) is simplified to O(1) because constants are irrelevant in asymptotic analysis. +// Big-O focuses on how resource usage scales with input size, not fixed values. + +// Using 2 variables: O(1) +// Using 10 variables: O(1) +// Using 100 variables: O(1) + +// What Space Complexity Looks Like for Larger Growth +// O(n): Memory grows linearly with the input size (e.g., storing an array of n elements). +// O(n^2): Memory grows quadratically (e.g., a 2D matrix with n*n elements). +// 𝑂(log 𝑛): Memory grows logarithmically (e.g., recursive calls in binary search). +// O(1): Fixed memory usage, regardless of input size (e.g., using a fixed number of variables). + +