diff --git a/best-time-to-buy-and-sell-stock/HoonDongKang.ts b/best-time-to-buy-and-sell-stock/HoonDongKang.ts new file mode 100644 index 000000000..1e2df325d --- /dev/null +++ b/best-time-to-buy-and-sell-stock/HoonDongKang.ts @@ -0,0 +1,40 @@ +/** + * [Problem]: [121] Best Time to Buy and Sell Stock + * + * (https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) + */ +function maxProfit(prices: number[]): number { + //시간복잡도: O(n^2); + //공간복잡도: O(1); + // Time Limit Exceeded + function doublyLoopFunc(prices: number[]): number { + let result = 0; + for (let i = 0; i < prices.length; i++) { + for (let j = i + 1; j < prices.length; j++) { + let profit = prices[j] - prices[i]; + result = Math.max(profit, result); + } + } + + return result; + } + + // 시간 복잡도: O(n) + // 공간 복잡도: O(1) + function twoPointerFunc(prices: number[]): number { + let minPrice = prices[0]; + let maxProfit = 0; + + for (let i = 1; i < prices.length; i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + maxProfit = Math.max(maxProfit, prices[i] - minPrice); + } + } + + return maxProfit; + } + + return twoPointerFunc(prices); +} diff --git a/encode-and-decode-strings/HoonDongKang.ts b/encode-and-decode-strings/HoonDongKang.ts new file mode 100644 index 000000000..9d585e259 --- /dev/null +++ b/encode-and-decode-strings/HoonDongKang.ts @@ -0,0 +1,37 @@ +/** + * [Problem]: [659] Encode and Decode Strings + * + * (https://www.lintcode.com/problem/659/) + */ + +//시간복잡도 O(n) +//공간복잡도 O(1) + +function encode(strs: string[]): string { + return strs.join("😄"); +} +function decode(s: string): string[] { + return s.split("😄"); +} + +// 시간복잡도: O(n) +// 공간복잡도: O(n) +function encode(strs: string[]): string { + return strs.map((str) => `${str.length}:${str}`).join(""); +} + +// 시간복잡도: O(n) +// 공간복잡도: O(n) +function decode(str: string): string[] { + const result: string[] = []; + let index = 0; + while (index < str.length) { + const separatorIndex = str.indexOf(":", index); + const length = +str.slice(index, separatorIndex); + const endIndex = separatorIndex + 1 + length; + + result.push(str.slice(separatorIndex + 1, endIndex)); + index = endIndex; + } + return result; +} diff --git a/group-anagrams/HoonDongKang.ts b/group-anagrams/HoonDongKang.ts new file mode 100644 index 000000000..58ac81fb8 --- /dev/null +++ b/group-anagrams/HoonDongKang.ts @@ -0,0 +1,40 @@ +/** + * [Problem]: [49] Group Anagrams + * + * (https://leetcode.com/problems/group-anagrams/description/) + */ +function groupAnagrams(strs: string[]): string[][] { + //시간복잡도 O(n * w log w) + //공간복잡도 O(n * w) + function mappedFunc(strs: string[]): string[][] { + const map = new Map(); + + for (let str of strs) { + let sorted = [...str].sort().join(""); + + map.set(sorted, [...(map.get(sorted) ?? []), str]); + } + + return [...map.values()]; + } + + //시간복잡오 O(n) + //공간복잡도 O(n * w) + function hashedMappedFunc(strs: string[]): string[][] { + const map = new Map(); + + for (let str of strs) { + const count: number[] = new Array(26).fill(0); + + for (const char of str) { + count[char.charCodeAt(0) - 97]++; + } + + const key = count.join("#"); + map.set(key, [...(map.get(key) ?? []), str]); + } + + return [...map.values()]; + } + return hashedMappedFunc(strs); +}