From 09b75dc610378c8dd12cc868afbb75093a603d2e Mon Sep 17 00:00:00 2001 From: choidabom Date: Mon, 6 Jan 2025 11:28:04 +0900 Subject: [PATCH 1/4] feat: 121. Best Time to Buy and Sell Stock --- best-time-to-buy-and-sell-stock/choidabom.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/choidabom.ts diff --git a/best-time-to-buy-and-sell-stock/choidabom.ts b/best-time-to-buy-and-sell-stock/choidabom.ts new file mode 100644 index 000000000..f7f08d589 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/choidabom.ts @@ -0,0 +1,20 @@ +/** + * Runtime: 6ms, Memory: 62.30MB + * + * Time Complexity: O(N) + * Space Complexity: O(1) + */ + +function maxProfit(prices: number[]): number { + let buyingPrice = prices[0]; + let profit = 0; + + for (const price of prices) { + buyingPrice = Math.min(price, buyingPrice); + profit = Math.max(profit, price - buyingPrice); + } + + return profit; +} + +maxProfit([7, 1, 5, 3, 6, 4]); From bb30d81ec6f182f0359b5fd66afcb8bf22489282 Mon Sep 17 00:00:00 2001 From: choidabom Date: Tue, 7 Jan 2025 12:43:29 +0900 Subject: [PATCH 2/4] add: problem link --- best-time-to-buy-and-sell-stock/choidabom.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/best-time-to-buy-and-sell-stock/choidabom.ts b/best-time-to-buy-and-sell-stock/choidabom.ts index f7f08d589..42b4a7abe 100644 --- a/best-time-to-buy-and-sell-stock/choidabom.ts +++ b/best-time-to-buy-and-sell-stock/choidabom.ts @@ -1,4 +1,5 @@ /** + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ * Runtime: 6ms, Memory: 62.30MB * * Time Complexity: O(N) From ad8a118f1801c73d29cfbe265e797f1890be3acd Mon Sep 17 00:00:00 2001 From: choidabom Date: Tue, 7 Jan 2025 12:46:55 +0900 Subject: [PATCH 3/4] feat: 49. Group Anagrams --- group-anagrams/choidabom.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 group-anagrams/choidabom.ts diff --git a/group-anagrams/choidabom.ts b/group-anagrams/choidabom.ts new file mode 100644 index 000000000..6cb2507b8 --- /dev/null +++ b/group-anagrams/choidabom.ts @@ -0,0 +1,32 @@ +/** + * https://leetcode.com/problems/group-anagrams/description/ + * Runtime: 27ms, Memory: 64.26MB + * + * Time Complexity: O(n * m * log m) + * Space Complexity: O(n * m) + * + * 접근 + * anagrams 관계인 단어는 정렬 시 모두 동일한 단어가 된다. + * 그러므로 정렬된 단어가 key 값이 되고, str을 정렬했을 때 key값과 동일한 경우 value가 되도록 추가. + */ + +function groupAnagrams(strs: string[]): string[][] { + let answer = []; + const anagramMap = new Map(); + + for (let str of strs) { + const key = str.split("").sort().join(""); // O(m * log m) + if (!anagramMap.has(key)) { + anagramMap.set(key, []); + } + anagramMap.get(key)?.push(str); + } + + anagramMap.forEach((value) => { + answer.push(value); + }); + + return answer; +} + +groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); From efdefa1e6c5197a9af0a1a8b408cc5917cd7f756 Mon Sep 17 00:00:00 2001 From: choidabom Date: Wed, 8 Jan 2025 23:17:43 +0900 Subject: [PATCH 4/4] feat: Encode and Decode Strings --- encode-and-decode-strings/choidabom.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 encode-and-decode-strings/choidabom.ts diff --git a/encode-and-decode-strings/choidabom.ts b/encode-and-decode-strings/choidabom.ts new file mode 100644 index 000000000..02b1850ab --- /dev/null +++ b/encode-and-decode-strings/choidabom.ts @@ -0,0 +1,19 @@ +/** + * https://www.lintcode.com/problem/659/ + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + +function encode(strs: string[]): string { + return strs.join("😀"); +} + +function decode(str: string): string[] { + return str.split("😀"); +} + +decode(encode(["Hello", "World"])); +/* + * output + * ["Hello","World"] + */