From 7a6663c48e3d14dd4fbf3762d2f78b6924096b65 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Tue, 17 Jun 2025 11:41:24 +0900 Subject: [PATCH 1/5] add Same Tree solution --- same-tree/HoonDongKang.ts | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 same-tree/HoonDongKang.ts diff --git a/same-tree/HoonDongKang.ts b/same-tree/HoonDongKang.ts new file mode 100644 index 000000000..3cbf81b86 --- /dev/null +++ b/same-tree/HoonDongKang.ts @@ -0,0 +1,43 @@ +/** + * [Problem]: [100] Same Tree + * (https://leetcode.com/problems/same-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; + } +} + +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + // 시간복잡도 O(N) + // 공간복잡도 O(N) + function recursiveFunc(p: TreeNode | null, q: TreeNode | null): boolean { + if (!p && !q) return true; + if (!p || !q) return false; + if (p.val !== q.val) return false; + return recursiveFunc(p.left, q.left) && recursiveFunc(p.right, q.right); + } + + // 시간복잡도 O(N) + // 공간복잡도 O(N) + function stackFunc(p: TreeNode | null, q: TreeNode | null): boolean { + const stack = [[p, q]]; + while (stack.length) { + const [p, q] = stack.pop()!; + if (!p && !q) continue; + if (!p || !q) return false; + if (p.val !== q.val) return false; + + stack.push([p.left, q.left]); + stack.push([p.right, q.right]); + } + + return true; + } +} From a74c82a406800ddf521a3e7c72bdc22e9b07d899 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Tue, 17 Jun 2025 11:41:33 +0900 Subject: [PATCH 2/5] add Remove Nth Node From End of List solution --- .../HoonDongKang.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/HoonDongKang.ts diff --git a/remove-nth-node-from-end-of-list/HoonDongKang.ts b/remove-nth-node-from-end-of-list/HoonDongKang.ts new file mode 100644 index 000000000..1086101d5 --- /dev/null +++ b/remove-nth-node-from-end-of-list/HoonDongKang.ts @@ -0,0 +1,58 @@ +/** + * [Problem]: [19] Remove Nth Node From End of List + * (https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) + */ + +class ListNode { + val: number; + next: ListNode | null; + constructor(val?: number, next?: ListNode | null) { + this.val = val === undefined ? 0 : val; + this.next = next === undefined ? null : next; + } +} + +function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { + // 시간복잡도 O(N) + // 공간복잡도 O(1) + function lengthFunc(head: ListNode | null, n: number): ListNode | null { + let length = 0; + let current = head; + while (current) { + length++; + current = current.next; + } + + let dummy = new ListNode(0, head); + current = dummy; + + for (let i = 0; i < length - n; i++) { + current = current?.next!; + } + + current.next = current.next!.next || null; + + return dummy.next; + } + + // 시간복잡도 O(N) + // 공간복잡도 O(1) + function twoPointerFunc(head: ListNode | null, n: number): ListNode | null { + let first = head; + + for (let i = 0; i < n; i++) { + first = first?.next!; + } + + let dummy = new ListNode(0, head); + let second = dummy; + + while (first) { + first = first.next; + second = second.next!; + } + + second.next = second.next?.next || null; + return dummy.next; + } +} From ab27f8d4b53c639e675c45565cd498c1682af367 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Thu, 19 Jun 2025 10:58:44 +0900 Subject: [PATCH 3/5] add Number of Connected Components in an Undirected Graph solution --- .../HoonDongKang.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 number-of-connected-components-in-an-undirected-graph/HoonDongKang.ts diff --git a/number-of-connected-components-in-an-undirected-graph/HoonDongKang.ts b/number-of-connected-components-in-an-undirected-graph/HoonDongKang.ts new file mode 100644 index 000000000..558d6a4f8 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/HoonDongKang.ts @@ -0,0 +1,73 @@ +/** + * [Problem]: [3651] Number of Connected Components in an Undirected Graph + * (https://www.lintcode.com/problem/3651/) + */ +export class Solution { + /** + * @param n: the number of vertices + * @param edges: the edges of undirected graph + * @return: the number of connected components + */ + countComponents(n: number, edges: number[][]): number { + //시간복잡도 O(n + e) + // 공간복잡도 O(n + e) + function dfsFunc(n: number, edges: number[][]): number { + const visited = new Array(n).fill(false); + const graph: number[][] = Array.from({ length: n }, () => []); + + for (const [node, adjacent] of edges) { + graph[node].push(adjacent); + graph[adjacent].push(node); + } + + function dfs(node: number) { + visited[node] = true; + for (const neighbor of graph[node]) { + if (!visited[neighbor]) { + dfs(neighbor); + } + } + } + + let count = 0; + + for (let i = 0; i < n; i++) { + if (!visited[i]) { + count++; + dfs(i); + } + } + + return count; + } + // 시간복잡도 O(n + e) + // 공간복잡도 O(n + e) + function stackFunc(n: number, edges: number[][]): number { + const visited = new Array(n).fill(false); + const graph: number[][] = Array.from({ length: n }, () => []); + let count = 0; + + for (const [node, adjacent] of edges) { + graph[node].push(adjacent); + graph[adjacent].push(node); + } + + for (let i = 0; i < n; i++) { + if (visited[i]) continue; + count++; + const stack = [i]; + + while (stack.length) { + const node = stack.pop()!; + visited[node] = true; + + for (let adj of graph[node]) { + if (!visited[adj]) stack.push(adj); + } + } + } + + return count; + } + } +} From 8a26cf008262c9eb7a47ad5e4886333997f09422 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Thu, 19 Jun 2025 11:11:03 +0900 Subject: [PATCH 4/5] add Non-overlapping Intervals solution --- non-overlapping-intervals/HoonDongKang.ts | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 non-overlapping-intervals/HoonDongKang.ts diff --git a/non-overlapping-intervals/HoonDongKang.ts b/non-overlapping-intervals/HoonDongKang.ts new file mode 100644 index 000000000..6e869564f --- /dev/null +++ b/non-overlapping-intervals/HoonDongKang.ts @@ -0,0 +1,25 @@ +/** + * [Problem]: [435] Non-overlapping Intervals + * (https://leetcode.com/problems/non-overlapping-intervals/description/) + */ + +//시간복잡도 O(n log n) +//공간복잡도 O(1) +function eraseOverlapIntervals(intervals: number[][]): number { + intervals.sort((a, b) => a[1] - b[1]); + + let count = 0; + let prev = intervals[0][1]; + + for (let i = 1; i < intervals.length; i++) { + let [start, end] = intervals[i]; + + if (prev > start) { + count++; + } else { + prev = end; + } + } + + return count; +} From d60724ce4fdd521dac74a985eae6730eea3b1d0f Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Fri, 20 Jun 2025 11:32:23 +0900 Subject: [PATCH 5/5] add Serialize and Deserialize Binary Tree solution --- .../HoonDongKang.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 serialize-and-deserialize-binary-tree/HoonDongKang.ts diff --git a/serialize-and-deserialize-binary-tree/HoonDongKang.ts b/serialize-and-deserialize-binary-tree/HoonDongKang.ts new file mode 100644 index 000000000..79864e899 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/HoonDongKang.ts @@ -0,0 +1,54 @@ +/** + * [Problem]: [297] Serialize and Deserialize Binary Tree + * (\https://leetcode.com/problems/serialize-and-deserialize-binary-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; + } +} + +/* + * Encodes a tree to a single string. + */ +//시간복잡도 O(N) +//공간복잡도 O(N) +function serialize(root: TreeNode | null): string { + if (!root) return "NULL"; + + const left = serialize(root.left); + const right = serialize(root.right); + + return `${root.val},${left},${right}`; +} + +/* + * Decodes your encoded data to tree. + */ +//시간복잡도 O(N) +//공간복잡도 O(N) +function deserialize(data: string): TreeNode | null { + const values = data.split(","); + let index = 0; + + function dfs(): TreeNode | null { + if (values[index] === "NULL") { + index++; + return null; + } + const node = new TreeNode(+values[index++]); + + node.left = dfs(); + node.right = dfs(); + + return node; + } + + return dfs(); +}