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..42b4a7abe --- /dev/null +++ b/best-time-to-buy-and-sell-stock/choidabom.ts @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ + * 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]); 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"] + */ 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"]);