Skip to content
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

[Helena] Week 4 solutions #96

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions counting-bits/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

var countBits = function(n) {
// initialize an array to hold the result.
let ans = new Array(n + 1).fill(0);

// iterate through all numbers from 1 to n.
for (let i = 1; i <= n; i++) {
ans[i] = ans[i >> 1] + (i & 1);
}
return ans;
};
25 changes: 25 additions & 0 deletions group-anagrams/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity: O(n * k)
// Space Complexity: O(n * k)

var groupAnagrams = function(strs) {
const map = new Map();
for (const str of strs) {
// initialize an array to count the frequency of characters.
const count = new Array(26).fill(0);
// increment the count for each character.
for (const char of str) {
count[char.charCodeAt(0) - 'a'.charCodeAt(0)]++;
Copy link
Contributor

Choose a reason for hiding this comment

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

ASCII 코드를 활용해서 인덱스 값을 주는 방법도 가능하군요!
좋은 접근방법인 것 같습니다.

}
// generate a unique key based on character frequencies.
const key = count.join('#');
// if the key exists, push the original string to its group.
if (map.has(key)) {
map.get(key).push(str);
} else { // else, create a new group with the key.
map.set(key, [str]);
}
}

// return the groups of the map as an array.
return Array.from(map.values());
};
13 changes: 13 additions & 0 deletions missing-number/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

var missingNumber = function(nums) {
const n = nums.length;
// sum of numbers from 0 to n.
const expectedSum = (n * (n + 1)) / 2;
// sum of numbers in the array.
const actualSum = nums.reduce((acc, curr) => acc + curr, 0);

// the difference is the missing number.
return expectedSum - actualSum;
};
14 changes: 14 additions & 0 deletions number-of-1-bits/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Time Complexity: O(log n)
// Space Complexity: O(1)

var hammingWeight = function(n) {
let count = 0;
while (n !== 0) {
// add 1 to count if the last bit is 1.
count += n & 1;
// unsigned right shift to process the next bit.
n = n >>> 1;
}

return count;
};
16 changes: 16 additions & 0 deletions reverse-bits/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time Complexity: O(1)
// Space Complexity: O(1)

var reverseBits = function(n) {
let result = 0;
// iterate 32 times since it's a 32-bit integer.
for (let i = 0; i < 32; i++) {
// shift the result to the left by 1 bit and OR it with the least significant bit of n.
result = (result << 1) | (n & 1);
// right shift n by 1 to consider the next bit.
n >>>= 1;
}

// convert to unsigned integer.
return result >>> 0;
Copy link
Member

Choose a reason for hiding this comment

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

>>>가 right shift 연산이라면, 0을 해야하는 이유가 있는건지 궁금해요.
이미 result는 양수의 값을 갖고 있으므로 unsigned integer로 만들 것 없이 그냥 반환해도 괜찮지 않을까 생각이 들었거든요.
(JS를 몰라서 제가 예단하고 있는 거겠지만요 ㅎㅎ)
만약 하지 않았을 경우 잘못된 정답이 리턴될 수도 있나요?

};