From 348c154f4ce589a388d0204973f2ab3a0b16ba23 Mon Sep 17 00:00:00 2001 From: mykoo Date: Sat, 14 Sep 2024 15:15:29 +0900 Subject: [PATCH 1/3] best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/GUMUNYEONG.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/GUMUNYEONG.js diff --git a/best-time-to-buy-and-sell-stock/GUMUNYEONG.js b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js new file mode 100644 index 000000000..548e4faa8 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let max = 0; + + for (let i = 1; i <= prices.length; i++) { + for (let j = i + 1; j <= prices.length; j++) { + const profit = prices[j] - prices[i]; + max = profit > max ? profit : max; + } + } + + return max; +}; + From 3cd7442b771190eb24a7634c678c132d28fd1a1c Mon Sep 17 00:00:00 2001 From: mykoo Date: Sat, 14 Sep 2024 15:20:39 +0900 Subject: [PATCH 2/3] best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/GUMUNYEONG.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/best-time-to-buy-and-sell-stock/GUMUNYEONG.js b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js index 548e4faa8..9e39b5077 100644 --- a/best-time-to-buy-and-sell-stock/GUMUNYEONG.js +++ b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js @@ -15,3 +15,5 @@ var maxProfit = function (prices) { return max; }; +// TC : O(n^2); +// SC : O(1); From 06ae7fdfd0d9fe1e8f6cc611042707997e3d46c1 Mon Sep 17 00:00:00 2001 From: mykoo Date: Sun, 15 Sep 2024 14:31:27 +0900 Subject: [PATCH 3/3] best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/GUMUNYEONG.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/best-time-to-buy-and-sell-stock/GUMUNYEONG.js b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js index 9e39b5077..c5a057ea0 100644 --- a/best-time-to-buy-and-sell-stock/GUMUNYEONG.js +++ b/best-time-to-buy-and-sell-stock/GUMUNYEONG.js @@ -5,7 +5,7 @@ var maxProfit = function (prices) { let max = 0; - for (let i = 1; i <= prices.length; i++) { + for (let i = 0; i <= prices.length; i++) { for (let j = i + 1; j <= prices.length; j++) { const profit = prices[j] - prices[i]; max = profit > max ? profit : max;