From f45731b9575cdadae96a0602dda3ec3d8349b38b Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Tue, 10 Jun 2025 00:03:06 +0900 Subject: [PATCH 1/5] add Missing Number solution --- missing-number/HoonDongKang.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 missing-number/HoonDongKang.ts diff --git a/missing-number/HoonDongKang.ts b/missing-number/HoonDongKang.ts new file mode 100644 index 000000000..320ff29f1 --- /dev/null +++ b/missing-number/HoonDongKang.ts @@ -0,0 +1,26 @@ +/** + * [Problem]: [268] Missing Number + * (https://leetcode.com/problems/missing-number/description/) + */ +function missingNumber(nums: number[]): number { + //시간복잡도 O(n^2) + //공간복잡도 O(1) + function loopFunc(nums: number[]): number { + let num = 0; + while (true) { + if (!nums.includes(num)) break; + num++; + } + + return num; + } + //시간복잡도 O(n) + //공간복잡도 O(1) + function sumFunc(nums: number[]): number { + const n = nums.length; + const sum = nums.reduce((acc, cur) => (acc += cur), 0); + const expected = (n * (n + 1)) / 2; + + return expected - sum; + } +} From a84c382381c2b682fb5485cefba4df3010abcfee Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Tue, 10 Jun 2025 11:03:28 +0900 Subject: [PATCH 2/5] add Reorder List solution --- reorder-list/HoonDongKang.ts | 84 ++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 reorder-list/HoonDongKang.ts diff --git a/reorder-list/HoonDongKang.ts b/reorder-list/HoonDongKang.ts new file mode 100644 index 000000000..4340719a3 --- /dev/null +++ b/reorder-list/HoonDongKang.ts @@ -0,0 +1,84 @@ +/** + * [Problem]: [143] Reorder List + * (https://leetcode.com/problems/reorder-list/) + */ + +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; + } +} + +/** + Do not return anything, modify head in-place instead. + */ +function reorderList(head: ListNode | null): void { + //시간복잡도 O(n) + //공간복잡도 O(n) + function stackFunc(head: ListNode | null): void { + if (!head || !head.next) return; + + const stack: ListNode[] = []; + let node: ListNode | null = head; + + while (node) { + stack.push(node); + node = node.next; + } + + const length = stack.length; + node = head; + + for (let i = 0; i < Math.floor(length / 2); i++) { + const tail = stack.pop()!; + const next: ListNode | null = node.next; + + node.next = tail; + tail.next = next; + + node = next!; + } + + node.next = null; + } + //시간복잡도 O(n) + //공간복잡도 O(1) + function twoPointerFunc(head: ListNode | null): void { + if (!head || !head.next) return; + + let slow = head; + let fast = head; + while (fast && fast.next && fast.next.next) { + slow = slow.next!; + fast = fast.next.next; + } + + let prev: ListNode | null = null; + let curr = slow.next; + slow.next = null; + + while (curr) { + const next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + + let first = head; + let second = prev; + + while (second) { + const tmp1 = first!.next; + const tmp2 = second.next; + + first!.next = second; + second.next = tmp1; + + first = tmp1!; + second = tmp2; + } + } +} From 6d67a79d280d40d1d37d4089947178a35f3c1d21 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Wed, 11 Jun 2025 11:05:41 +0900 Subject: [PATCH 3/5] add Graph Valid Tree solution --- graph-valid-tree/HoonDongKang.ts | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 graph-valid-tree/HoonDongKang.ts diff --git a/graph-valid-tree/HoonDongKang.ts b/graph-valid-tree/HoonDongKang.ts new file mode 100644 index 000000000..026374dc8 --- /dev/null +++ b/graph-valid-tree/HoonDongKang.ts @@ -0,0 +1,65 @@ +/** + * [Problem]: [178] Graph Valid Tree + * (https://www.lintcode.com/problem/178/) + */ + +export class Solution { + /** + * @param n: An integer + * @param edges: a list of undirected edges + * @return: true if it's a valid tree, or false + */ + //시간복잡도 O(n+e) + //공간복잡도 O(n+e) + validTree(n: number, edges: number[][]): boolean { + const graph: number[][] = Array.from({ length: n }, () => []); + for (const [a, b] of edges) { + graph[a].push(b); + graph[b].push(a); + } + + const visited = new Set(); + + function hasCycle(node: number, prev: number): boolean { + if (visited.has(node)) return true; + visited.add(node); + + for (const neighbor of graph[node]) { + if (neighbor === prev) continue; + if (hasCycle(neighbor, node)) return true; + } + return false; + } + + if (hasCycle(0, -1)) return false; + + return visited.size === n; + } + + //시간복잡도 O(n) + //공간복잡도 O(n) + validTree2(n: number, edges: number[][]): boolean { + if (edges.length !== n - 1) return false; + + const graph: number[][] = Array.from({ length: n }, () => []); + for (const [a, b] of edges) { + graph[a].push(b); + graph[b].push(a); + } + + const visited = new Set(); + + function dfs(node: number) { + visited.add(node); + for (const neighbor of graph[node]) { + if (!visited.has(neighbor)) { + dfs(neighbor); + } + } + } + + dfs(0); + + return visited.size === n; + } +} From 3092dfbef8317884bc4ca4b931bb218711a7d52f Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Thu, 12 Jun 2025 11:43:56 +0900 Subject: [PATCH 4/5] add Merge Intervals solution --- merge-intervals/HoonDongKang.ts | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 merge-intervals/HoonDongKang.ts diff --git a/merge-intervals/HoonDongKang.ts b/merge-intervals/HoonDongKang.ts new file mode 100644 index 000000000..b03cdd37f --- /dev/null +++ b/merge-intervals/HoonDongKang.ts @@ -0,0 +1,58 @@ +/** + * [Problem]: [56] Merge Intervals + * (https://leetcode.com/problems/merge-intervals/description/) + */ +function merge(intervals: number[][]): number[][] { + //시간복잡도 O(n^2) + //공간복잡도 O(n) + function bruteForceFunc(intervals: number[][]): number[][] { + const result: number[][] = []; + const visited = new Set(); + + for (let i = 0; i < intervals.length; i++) { + if (visited.has(i)) continue; + visited.add(i); + + while (true) { + let hasMerged = false; + for (let j = 0; j < intervals.length; j++) { + if (visited.has(j)) continue; + + const [curA, curB] = intervals[i]; + const [nextA, nextB] = intervals[j]; + + if (curA <= nextB && nextA <= curB) { + visited.add(j); + intervals[i] = [Math.min(curA, nextA), Math.max(curB, nextB)]; + + hasMerged = true; + } + } + if (!hasMerged) break; + } + result.push(intervals[i]); + } + return result; + } + + //시간복잡도 O(nlog n) + //공간복잡도 O(n) + function sortFunc(intervals: number[][]): number[][] { + if (!intervals.length) return intervals; + intervals.sort((a, b) => a[0] - b[0]); + + const result: number[][] = [intervals[0]]; + for (let i = 0; i < intervals.length; i++) { + const prev = result[result.length - 1]; + const cur = intervals[i]; + + if (cur[0] <= prev[1]) { + prev[1] = Math.max(prev[1], cur[1]); + } else { + result.push(cur); + } + } + + return result; + } +} From bfe031966af8b7029a4a4b78f4032007034558f0 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Fri, 13 Jun 2025 10:49:50 +0900 Subject: [PATCH 5/5] add Binary Tree Maximum Path Sum solution --- binary-tree-maximum-path-sum/HoonDongKang.ts | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 binary-tree-maximum-path-sum/HoonDongKang.ts diff --git a/binary-tree-maximum-path-sum/HoonDongKang.ts b/binary-tree-maximum-path-sum/HoonDongKang.ts new file mode 100644 index 000000000..ae6f8054d --- /dev/null +++ b/binary-tree-maximum-path-sum/HoonDongKang.ts @@ -0,0 +1,38 @@ +/** + * [Problem]: [124 Binary Tree Maximum Path Sum + * (https://leetcode.com/problems/binary-tree-maximum-path-sum/description/) + */ +/** + * Definition for a binary tree node. + * 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(h) +function maxPathSum(root: TreeNode | null): number { + if (!root) return 0; + let result = root.val; + + function dfs(node: TreeNode | null) { + if (!node) return 0; + + let leftMax = Math.max(dfs(node.left), 0); + let rightMax = Math.max(dfs(node.right), 0); + + result = Math.max(node.val + leftMax + rightMax, result); + + return node.val + Math.max(leftMax, rightMax); + } + + dfs(root); + return result; +}