diff --git a/alien-dictionary/HoonDongKang.ts b/alien-dictionary/HoonDongKang.ts new file mode 100644 index 000000000..b7178ebdf --- /dev/null +++ b/alien-dictionary/HoonDongKang.ts @@ -0,0 +1,75 @@ +/** + * [Problem]: [892] Alien Dictionary + * (https://www.lintcode.com/problem/892/) + */ + +//시간복잡도 O(n+e) +//공간복잡도 O(n+e) +export class Solution { + /** + * @param words: a list of words + * @return: a string which is correct order + */ + alienOrder(words: string[]): string { + // Write your code here + const graph: Map> = new Map(); + + // 모든 문자를 그래프 노드로 초기화 + for (const word of words) { + for (const char of word) { + if (!graph.has(char)) { + graph.set(char, new Set()); + } + } + } + + // 문자 간 선후관계(edge) 설정 + for (let i = 1; i < words.length; i++) { + const prev = words[i - 1]; + const curr = words[i]; + let isDiff = false; + + for (let j = 0; j < Math.min(prev.length, curr.length); j++) { + const p = prev[j]; + const c = curr[j]; + + if (p !== c) { + graph.get(p)!.add(c); + isDiff = true; + break; + } + } + + if (!isDiff && prev.length > curr.length) { + return ""; + } + } + + const visited: Set = new Set(); + const visiting: Set = new Set(); + const output: string[] = []; + + function dfs(node: string): boolean { + if (visited.has(node)) return true; + if (visiting.has(node)) return false; + + visiting.add(node); + + for (const neighbor of graph.get(node)!) { + if (!dfs(neighbor)) return false; + } + + visiting.delete(node); + visited.add(node); + output.push(node); + + return true; + } + + for (const node of graph.keys()) { + if (!dfs(node)) return ""; + } + + return output.reverse().join(""); + } +} diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/HoonDongKang.ts b/construct-binary-tree-from-preorder-and-inorder-traversal/HoonDongKang.ts new file mode 100644 index 000000000..572c965e2 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/HoonDongKang.ts @@ -0,0 +1,41 @@ +/** + * [Problem]: [105] Construct Binary Tree From Preorder And Inorder Traversal + * (https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val === undefined ? 0 : val; + this.left = left === undefined ? null : left; + this.right = right === undefined ? null : right; + } +} + +//시간복잡도 O(n) +//공간복잡도 O(n) +function buildTree(preorder: number[], inorder: number[]): TreeNode | null { + if (!preorder.length || !inorder.length) return null; + + const map = new Map(); + let idx = 0; + + inorder.forEach((v, i) => map.set(v, i)); + + function dfs(left: number, right: number): TreeNode | null { + if (left > right) return null; + + const value = preorder[idx++]; + const root = new TreeNode(value); + const index = map.get(value)!; + + root.left = dfs(left, index - 1); + root.right = dfs(index + 1, right); + + return root; + } + + return dfs(0, inorder.length - 1); +} diff --git a/longest-palindromic-substring/HoonDongKang.ts b/longest-palindromic-substring/HoonDongKang.ts new file mode 100644 index 000000000..98ecf666e --- /dev/null +++ b/longest-palindromic-substring/HoonDongKang.ts @@ -0,0 +1,31 @@ +/** + * [Problem]: [5] Longest Palindromic Substring + * (https://leetcode.com/problems/longest-palindromic-substring/) + */ +// 시간복잡도 O(n^2) +// 공간복잡도 O(1) +function longestPalindrome(s: string): string { + if (s.length < 2) return s; + + let start = 0; + let end = 0; + + function findPalindrome(left: number, right: number): void { + while (0 <= left && right < s.length && s[left] === s[right]) { + left--; + right++; + } + + if (right - left - 1 > end - start) { + start = left + 1; + end = right - 1; + } + } + + for (let i = 0; i < s.length; i++) { + findPalindrome(i, i); + findPalindrome(i, i + 1); + } + + return s.substring(start, end + 1); +} diff --git a/rotate-image/HoonDongKang.ts b/rotate-image/HoonDongKang.ts new file mode 100644 index 000000000..ee0113941 --- /dev/null +++ b/rotate-image/HoonDongKang.ts @@ -0,0 +1,22 @@ +/** + * [Problem]: [48] Rotate Image + * (https://leetcode.com/problems/rotate-image/) + */ +/** + Do not return anything, modify matrix in-place instead. + */ +// 시간복잡도 O(n^2) +// 공간복잡도 O(1) +function rotate(matrix: number[][]): void { + const length = matrix.length; + + for (let i = 0; i < length; i++) { + for (let j = i + 1; j < length; j++) { + [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]; + } + } + + for (let i = 0; i < length; i++) { + matrix[i].reverse(); + } +} diff --git a/subtree-of-another-tree/HoonDongKang.ts b/subtree-of-another-tree/HoonDongKang.ts new file mode 100644 index 000000000..6adb2b194 --- /dev/null +++ b/subtree-of-another-tree/HoonDongKang.ts @@ -0,0 +1,26 @@ +/** + * [Problem]: [572] Subtree of Another Tree + * (https://leetcode.com/problems/subtree-of-another-tree/description/) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val === undefined ? 0 : val; + this.left = left === undefined ? null : left; + this.right = right === undefined ? null : right; + } +} + +//시간복잡도 O(n*m) +//공간복잡도 O(n+m) +function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean { + const pre = (node: TreeNode | null) => { + if (!node) return "N"; + return `(${node.val},${pre(node.left)},${pre(node.right)})`; + }; + + return pre(root).includes(pre(subRoot)); +}