From 9fac6a2738b1d5d1f65619bd23227ace0efb1f10 Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sat, 28 Mar 2026 00:25:56 +0900 Subject: [PATCH 1/3] week4: merge-two-sorted-lists --- merge-two-sorted-lists/reeseo3o.js | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 merge-two-sorted-lists/reeseo3o.js diff --git a/merge-two-sorted-lists/reeseo3o.js b/merge-two-sorted-lists/reeseo3o.js new file mode 100644 index 0000000000..7b613d7802 --- /dev/null +++ b/merge-two-sorted-lists/reeseo3o.js @@ -0,0 +1,42 @@ +/** + * 1. Recursive approach + * Time complexity: O(n + m) + * Space complexity: O(n + m) + */ +// const mergeTwoLists = (list1, list2) => { +// if (!list1) return list2; +// if (!list2) return list1; +// +// if (list1.val <= list2.val) { +// list1.next = mergeTwoLists(list1.next, list2); +// return list1; +// } else { +// list2.next = mergeTwoLists(list1, list2.next); +// return list2; +// } +// }; + +/** + * 2. Iterative approach + Dummy Node + * Time complexity: O(n + m) + * Space complexity: O(1) + */ +const mergeTwoLists = (list1, list2) => { + const dummy = new ListNode(-1); + let current = dummy; + + while (list1 !== null && list2 !== null) { + if (list1.val <= list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + + current.next = list1 ?? list2; + + return dummy.next; +}; From 68408c98da1827dd472c7603d9e1b361f566b265 Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sat, 28 Mar 2026 18:37:38 +0900 Subject: [PATCH 2/3] week4: maximum-subarray --- maximum-subarray/reeseo3o.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 maximum-subarray/reeseo3o.js diff --git a/maximum-subarray/reeseo3o.js b/maximum-subarray/reeseo3o.js new file mode 100644 index 0000000000..a01f3711c5 --- /dev/null +++ b/maximum-subarray/reeseo3o.js @@ -0,0 +1,34 @@ +/** + * Time complexity: O(n) + * Space complexity: O(h) - h is the height of the tree, worst case O(n) + */ +const maxDepth = (root) => { + if (root === null) return 0; + + const leftDepth = maxDepth(root.left); + const rightDepth = maxDepth(root.right); + + return 1 + Math.max(leftDepth, rightDepth); +}; + +// BFS - TC: O(n) | SC: O(n) +const maxDepthBFS = (root) => { + if (root === null) return 0; + + const queue = [root]; + let depth = 0; + + while (queue.length > 0) { + const levelSize = queue.length; + + for (let i = 0; i < levelSize; i++) { + const node = queue.shift(); + if (node.left) queue.push(node.left); + if (node.right) queue.push(node.right); + } + + depth++; + } + + return depth; +}; From fde6a4a74b5965d0bf91efe65a6cb6571dc5f1cd Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sat, 28 Mar 2026 18:40:24 +0900 Subject: [PATCH 3/3] docs: add complexity comments for maximum-subarray --- maximum-subarray/reeseo3o.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/maximum-subarray/reeseo3o.js b/maximum-subarray/reeseo3o.js index a01f3711c5..1332a6f056 100644 --- a/maximum-subarray/reeseo3o.js +++ b/maximum-subarray/reeseo3o.js @@ -1,4 +1,5 @@ -/** +/** + * DFS * Time complexity: O(n) * Space complexity: O(h) - h is the height of the tree, worst case O(n) */ @@ -11,7 +12,11 @@ const maxDepth = (root) => { return 1 + Math.max(leftDepth, rightDepth); }; -// BFS - TC: O(n) | SC: O(n) +/** + * BFS + * Time complexity: O(n) + * Space complexity: O(n) + */ const maxDepthBFS = (root) => { if (root === null) return 0;