-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #824 from yoonthecoder/main
[yoonthecoder] Week 4
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |