From af47da3d450eaefa2684199dff66ce44b2c9eb34 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Wed, 11 Sep 2024 11:53:24 +0900 Subject: [PATCH 1/3] Best Time to Buy And Sell Stock --- best-time-to-buy-and-sell-stock/hyejjun.js | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/hyejjun.js diff --git a/best-time-to-buy-and-sell-stock/hyejjun.js b/best-time-to-buy-and-sell-stock/hyejjun.js new file mode 100644 index 000000000..048b453d0 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hyejjun.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let minPrice = Infinity; + let maxProfit = 0; + + for (let i = 0; i < prices.length; i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + maxProfit = Math.max(maxProfit, prices[i] - minPrice); + } + } + + return maxProfit; + +}; + +console.log(maxProfit([7, 1, 5, 3, 6, 4])); +console.log(maxProfit([7, 6, 4, 3, 1])); + +/* +시간 복잡도: O(n) +공간 복잡도: O(1) +*/ From facd449bd8dcba7a8bf34f6154abf2cdfe862b0c Mon Sep 17 00:00:00 2001 From: hyejjun Date: Wed, 11 Sep 2024 12:17:42 +0900 Subject: [PATCH 2/3] Group Anagrams --- group-anagrams/hyejjun.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 group-anagrams/hyejjun.js diff --git a/group-anagrams/hyejjun.js b/group-anagrams/hyejjun.js new file mode 100644 index 000000000..9bf4980f6 --- /dev/null +++ b/group-anagrams/hyejjun.js @@ -0,0 +1,29 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + let map = new Map(); + + for (let str of strs) { + let sortedStr = str.split('').sort().join(''); + + if (map.has(sortedStr)) { + map.get(sortedStr).push(str); + } else { + map.set(sortedStr, [str]); + } + } + + return Array.from(map.values()); +}; + +console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); +console.log(groupAnagrams([""])); +console.log(groupAnagrams(["a"])); + + +/* +시간 복잡도: O(n*k log k) +공간 복잡도: O(n*k) +*/ From b363381b3b64dea5f94bc1d647d50da34ea3b98f Mon Sep 17 00:00:00 2001 From: hyejjun Date: Sat, 14 Sep 2024 13:14:17 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20-=20=EC=B6=94=EA=B0=80=20=EC=84=A4=EB=AA=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group-anagrams/hyejjun.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/group-anagrams/hyejjun.js b/group-anagrams/hyejjun.js index 9bf4980f6..8d9f11a97 100644 --- a/group-anagrams/hyejjun.js +++ b/group-anagrams/hyejjun.js @@ -26,4 +26,7 @@ console.log(groupAnagrams(["a"])); /* 시간 복잡도: O(n*k log k) 공간 복잡도: O(n*k) + +n: 문자열의 개수 +k: 문자열의 최대 길이 */