From 514f317f1294a8a4527dc60ce928f6b990b95394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Mon, 9 Sep 2024 23:49:03 +0900 Subject: [PATCH 1/2] Add week 5 solutions: best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/gitsunmin.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/gitsunmin.ts diff --git a/best-time-to-buy-and-sell-stock/gitsunmin.ts b/best-time-to-buy-and-sell-stock/gitsunmin.ts new file mode 100644 index 000000000..a411f52d4 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/gitsunmin.ts @@ -0,0 +1,17 @@ +/** + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock + * time complexity : O(n) + * space complexity : O(1) + */ + +function maxProfit(prices: number[]): number { + let maxProfit = 0; + let [minPrice] = prices; + + for (let price of prices) { + maxProfit = Math.max(maxProfit, price - minPrice); + minPrice = Math.min(minPrice, price); + } + + return maxProfit; +}; From d63b92e2645c9d7434fc3df269f4606f613c993e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 10 Sep 2024 19:39:38 +0900 Subject: [PATCH 2/2] Add week 5 solutions: group-anagrams --- group-anagrams/gitsunmin.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 group-anagrams/gitsunmin.ts diff --git a/group-anagrams/gitsunmin.ts b/group-anagrams/gitsunmin.ts new file mode 100644 index 000000000..308a03f85 --- /dev/null +++ b/group-anagrams/gitsunmin.ts @@ -0,0 +1,17 @@ +/** + * https://leetcode.com/problems/group-anagrams + * time complexity : O(n * k log k) + * space complexity : O(n * k) + */ +function groupAnagrams(strs: string[]): string[][] { + const map = new Map(); + + for (const str of strs) { + const sortedStr = str.split("").sort().join(""); + + if (map.has(sortedStr)) map.get(sortedStr).push(str); + else map.set(sortedStr, [str]); + } + + return Array.from(map.values()); +};