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); +} diff --git a/implement-trie-prefix-tree/HoonDongKang.ts b/implement-trie-prefix-tree/HoonDongKang.ts new file mode 100644 index 000000000..4ba11cefc --- /dev/null +++ b/implement-trie-prefix-tree/HoonDongKang.ts @@ -0,0 +1,65 @@ +/** + * [Problem]: [208] Implement Trie (Prefix Tree) + * (https://leetcode.com/problems/implement-trie-prefix-tree/description/) + */ + +class TrieNode { + children: { [key: string]: TrieNode }; + isEnd: boolean; + + constructor() { + this.children = {}; + this.isEnd = false; + } +} + +class Trie { + constructor(private root = new TrieNode()) {} + + //시간복잡도: O(n) + //공간복잡도: O(n) + insert(word: string): void { + let node = this.root; + for (let char of word) { + if (!node.children[char]) { + node.children[char] = new TrieNode(); + } + + node = node.children[char]; + } + + node.isEnd = true; + } + + //시간복잡도: O(n) + //공간복잡도: O(n) + search(word: string): boolean { + let node = this.root; + for (let char of word) { + if (!node.children[char]) return false; + node = node.children[char]; + } + + return node.isEnd; + } + + //시간복잡도: O(n) + //공간복잡도: O(n) + startsWith(prefix: string): boolean { + let node = this.root; + for (let char of prefix) { + if (!node.children[char]) return false; + node = node.children[char]; + } + + return true; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ diff --git a/word-break/HoonDongKang.ts b/word-break/HoonDongKang.ts new file mode 100644 index 000000000..b035bcae5 --- /dev/null +++ b/word-break/HoonDongKang.ts @@ -0,0 +1,51 @@ +/** + * [Problem]: [139] Word Break + * (https://leetcode.com/problems/word-break/description/) + */ +function wordBreak(s: string, wordDict: string[]): boolean { + //시간복잡도: O(n^2) + //공간복잡도: O(n) + function dfsFunc(s: string, wordDict: string[]): boolean { + let wordSet = new Set(wordDict); + let memo = new Map(); + + function dfs(start: number): boolean { + if (start === s.length) return true; + if (memo.has(start)) return memo.get(start)!; + + for (let end = start + 1; end <= s.length; end++) { + const word = s.slice(start, end); + + if (wordSet.has(word) && dfs(end)) { + memo.set(start, true); + return true; + } + } + + memo.set(start, false); + return false; + } + + return dfs(0); + } + + //시간복잡도: O(n^2) + //공간복잡도: O(n) + function dpFunc(s: string, wordDict: string[]): boolean { + const wordSet = new Set(wordDict); + const dp = new Array(s.length + 1).fill(false); + dp[0] = true; + + for (let end = 1; end <= s.length; end++) { + for (let start = 0; start < end; start++) { + const isExists = wordSet.has(s.slice(start, end)); + if (isExists && dp[start]) { + dp[end] = true; + break; + } + } + } + return dp[s.length]; + } + return dpFunc(s, wordDict); +}