Skip to content

Commit

Permalink
Merge pull request #824 from yoonthecoder/main
Browse files Browse the repository at this point in the history
[yoonthecoder] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents f390a6f + 0e1b8fa commit d4a5f10
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions merge-two-sorted-lists/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions missing-number/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions two-sum/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d4a5f10

Please sign in to comment.