Skip to content

[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

Merged
merged 5 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/Jeehay28.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!
재귀도 효율적이지만 반복문으로 SC를 O(1)로 최적화하는 방법도 추가로 고려해보면 좋을 것 같습니다.

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;
}

};


68 changes: 68 additions & 0 deletions missing-number/Jeehay28.js
Copy link
Member

@dusunax dusunax Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My own approach부분도 잘 봤습니다
문제의 전제가 중복된 숫자가 없는 배열이라
let distinctNums = new Set(nums);으로 작성해도 동작하네요

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오.. XOR 풀이도 신기하네요!
이미 복잡도는 최대치이긴 하지만, 아래와 같이 순회하면 순회를 절반으로 줄여서 성능을 거의 두배로 올릴 수 있을것 같은데요?!

for (let i = 0; i < n; i++) {
        xor = xor ^ i ^ nums[i];
    }

// 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;
// }
// }

// };
Loading