-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] Week 4 #812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Jeehay28] Week 4 #812
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} list1 | ||
* @param {ListNode} list2 | ||
* @return {ListNode} | ||
*/ | ||
|
||
// Time Complexity: O(m + n) | ||
// Space Complexity: O(m + n) | ||
|
||
var mergeTwoLists = function(list1, list2) { | ||
|
||
|
||
if(!(list1 && list2)) { | ||
return list1 || list2; | ||
} | ||
|
||
if(list1.val < list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1; | ||
} else { | ||
list2.next = mergeTwoLists(list1, list2.next); | ||
return list2; | ||
} | ||
|
||
}; | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My own approach부분도 잘 봤습니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
|
||
// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums | ||
// XOR method | ||
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements) | ||
// Space complexity: O(1) | ||
|
||
var missingNumber = function (nums) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오.. XOR 풀이도 신기하네요!
|
||
// XOR with itself results in 0 : a xor a = 0 | ||
// XOR with 0 results in the number itself : a xor 0 = a | ||
// XOR is commutative and associative | ||
|
||
const n = nums.length; | ||
|
||
let xor = 0; | ||
|
||
for (let i = 0; i <= n; i++) { | ||
xor ^= i; | ||
} | ||
|
||
for (any of nums) { | ||
xor ^= any; | ||
} | ||
|
||
return xor; | ||
}; | ||
|
||
// *** Guided approach 1: simplicity and clarity | ||
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2 | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
// var missingNumber = function (nums) { | ||
// const n = nums.length; | ||
// const expectedSum = (n * (n + 1)) / 2; | ||
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n) | ||
|
||
// const missingNum = expectedSum - actualSum; | ||
|
||
// return missingNum; | ||
// }; | ||
|
||
// *** My own approach | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
// var missingNumber = function (nums) { | ||
|
||
// let distinctNums = new Set([]); | ||
|
||
// for (any of nums) { | ||
// if (distinctNums.has(any)) { | ||
// return | ||
// } else { | ||
// distinctNums.add(any) | ||
// } | ||
// } | ||
|
||
// const n = distinctNums.size; | ||
|
||
// for (let i = 0; i <= n; i++) { | ||
// if (!nums.includes(i)) { | ||
// return i; | ||
// } | ||
// } | ||
|
||
// }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다!
재귀도 효율적이지만 반복문으로 SC를 O(1)로 최적화하는 방법도 추가로 고려해보면 좋을 것 같습니다.