From d9fcd03aa372fb9ff3b9f273f0daef0ac8229194 Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Tue, 31 Dec 2024 14:09:44 -0800 Subject: [PATCH 1/3] merge two sorted lists solution --- merge-two-sorted-lists/yoonthecoder.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 merge-two-sorted-lists/yoonthecoder.js diff --git a/merge-two-sorted-lists/yoonthecoder.js b/merge-two-sorted-lists/yoonthecoder.js new file mode 100644 index 000000000..7e63fa58c --- /dev/null +++ b/merge-two-sorted-lists/yoonthecoder.js @@ -0,0 +1,25 @@ +var mergeTwoLists = function (list1, list2) { + // create a placeholder node and use it as a starting point + let placeholder = { val: -1, next: null }; + let current = placeholder; + + // loop through the lists until one of them is fully traversed + while (list1 !== null && list2 !== null) { + if (list1.val <= list2.val) { + // connect the element of list1 with the current node + current.next = list1; + // move list1 to its next node + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + // move the current pointer to the newly added node + current = current.next; + } + current.next = list1 !== null ? list1 : list2; + return placeholder.next; +}; + +// Time Complexity: O(n+m); +// Space Complexity: O(1) From e2b65073136872dbe39c1e0633c6743f42ec87ea Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Thu, 2 Jan 2025 02:11:18 -0800 Subject: [PATCH 2/3] missing number solution --- missing-number/yoonthecoder.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 missing-number/yoonthecoder.js diff --git a/missing-number/yoonthecoder.js b/missing-number/yoonthecoder.js new file mode 100644 index 000000000..6ce3190ce --- /dev/null +++ b/missing-number/yoonthecoder.js @@ -0,0 +1,14 @@ +var missingNumber = function (nums) { + // store all the elemenst from nums in a Set + const set = new Set(nums); + + // check the missing number by iterating through the index + for (i = 0; i <= nums.length; i++) { + if (!set.has(i)) { + return i; + } + } +}; + +// Time complexity: O(n); +// Space complexity: O(n); From 0e1b8fa33e49b6dbe85d4fcce697601a07a80d73 Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Thu, 2 Jan 2025 15:39:40 -0800 Subject: [PATCH 3/3] two sum solution --- two-sum/yoonthecoder.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 two-sum/yoonthecoder.js diff --git a/two-sum/yoonthecoder.js b/two-sum/yoonthecoder.js new file mode 100644 index 000000000..f37002549 --- /dev/null +++ b/two-sum/yoonthecoder.js @@ -0,0 +1,14 @@ +var twoSum = function (nums, target) { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) { + return [map.get(complement), i]; + } + map.set(nums[i], i); + } +}; + +// Time complexity: O(n) +// Space complexity: O(n) - hash map storage