From c4535e6bc9d26058d5b3dafdc929d2480bd0470c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 2 Nov 2024 10:02:41 +0900 Subject: [PATCH 1/3] Add week 12 solutions: same-tree --- same-tree/gitsunmin.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 same-tree/gitsunmin.ts diff --git a/same-tree/gitsunmin.ts b/same-tree/gitsunmin.ts new file mode 100644 index 000000000..fb5fe098e --- /dev/null +++ b/same-tree/gitsunmin.ts @@ -0,0 +1,24 @@ +/** + * https://leetcode.com/problems/same-tree/ + * time complexity : O(n) + * space complexity : O(n) + */ + +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 { + if (p === null && q === null) return true; + if (p === null || q === null) return false; + if (p.val !== q.val) return false; + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +}; From 773f1706d263b3088f6f1a5a1ad716210d69961f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 2 Nov 2024 10:14:04 +0900 Subject: [PATCH 2/3] Add week 12 solutions: remove-nth-node-from-end-of-list --- remove-nth-node-from-end-of-list/gitsunmin.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/gitsunmin.ts diff --git a/remove-nth-node-from-end-of-list/gitsunmin.ts b/remove-nth-node-from-end-of-list/gitsunmin.ts new file mode 100644 index 000000000..0db7d792f --- /dev/null +++ b/remove-nth-node-from-end-of-list/gitsunmin.ts @@ -0,0 +1,33 @@ +/** + * https://leetcode.com/problems/remove-nth-node-from-end-of-list/ + * time complexity : O(n) + * space complexity : O(1) + */ + +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 { + const dummy = new ListNode(0, head); + let first: ListNode | null = dummy; + let second: ListNode | null = dummy; + + for (let i = 0; i <= n; i++) { + if (first !== null) first = first.next; + } + + while (first !== null) { + first = first.next; + second = second!.next; + } + + if (second !== null && second.next !== null) second.next = second.next.next; + + return dummy.next; +}; From 1f50aa2ca45f5859074f45d4dd456a334289ffda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 2 Nov 2024 10:16:23 +0900 Subject: [PATCH 3/3] Add week 12 solutions: merge-intervals --- merge-intervals/gitsunmin.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 merge-intervals/gitsunmin.ts diff --git a/merge-intervals/gitsunmin.ts b/merge-intervals/gitsunmin.ts new file mode 100644 index 000000000..c5d1044c7 --- /dev/null +++ b/merge-intervals/gitsunmin.ts @@ -0,0 +1,18 @@ +/** + * https://leetcode.com/problems/merge-intervals/ + * time complexity : O(n) + * space complexity : O(n) + */ + +function merge(intervals: number[][]): number[][] { + if (intervals.length === 0) return []; + + intervals.sort((a, b) => a[0] - b[0]); + + const merged: number[][] = []; + for (const interval of intervals) { + if (merged.length === 0 || merged[merged.length - 1][1] < interval[0]) merged.push(interval); + else merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]); + } + return merged; +};