Skip to content

Commit 1698b27

Browse files
committed
Add week 4 solutions
1 parent e86ad1e commit 1698b27

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

counting-bits/yolophg.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var countBits = function(n) {
5+
// initialize an array to hold the result.
6+
let ans = new Array(n + 1).fill(0);
7+
8+
// iterate through all numbers from 1 to n.
9+
for (let i = 1; i <= n; i++) {
10+
ans[i] = ans[i >> 1] + (i & 1);
11+
}
12+
return ans;
13+
};

group-anagrams/yolophg.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time Complexity: O(n * k)
2+
// Space Complexity: O(n * k)
3+
4+
var groupAnagrams = function(strs) {
5+
const map = new Map();
6+
for (const str of strs) {
7+
// initialize an array to count the frequency of characters.
8+
const count = new Array(26).fill(0);
9+
// increment the count for each character.
10+
for (const char of str) {
11+
count[char.charCodeAt(0) - 'a'.charCodeAt(0)]++;
12+
}
13+
// generate a unique key based on character frequencies.
14+
const key = count.join('#');
15+
// if the key exists, push the original string to its group.
16+
if (map.has(key)) {
17+
map.get(key).push(str);
18+
} else { // else, create a new group with the key.
19+
map.set(key, [str]);
20+
}
21+
}
22+
23+
// return the groups of the map as an array.
24+
return Array.from(map.values());
25+
};

missing-number/yolophg.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var missingNumber = function(nums) {
5+
const n = nums.length;
6+
// sum of numbers from 0 to n.
7+
const expectedSum = (n * (n + 1)) / 2;
8+
// sum of numbers in the array.
9+
const actualSum = nums.reduce((acc, curr) => acc + curr, 0);
10+
11+
// the difference is the missing number.
12+
return expectedSum - actualSum;
13+
};

number-of-1-bits/yolophg.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time Complexity: O(log n)
2+
// Space Complexity: O(1)
3+
4+
var hammingWeight = function(n) {
5+
let count = 0;
6+
while (n !== 0) {
7+
// add 1 to count if the last bit is 1.
8+
count += n & 1;
9+
// unsigned right shift to process the next bit.
10+
n = n >>> 1;
11+
}
12+
13+
return count;
14+
};

reverse-bits/yolophg.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(1)
2+
// Space Complexity: O(1)
3+
4+
var reverseBits = function(n) {
5+
let result = 0;
6+
// iterate 32 times since it's a 32-bit integer.
7+
for (let i = 0; i < 32; i++) {
8+
// shift the result to the left by 1 bit and OR it with the least significant bit of n.
9+
result = (result << 1) | (n & 1);
10+
// right shift n by 1 to consider the next bit.
11+
n >>>= 1;
12+
}
13+
14+
// convert to unsigned integer.
15+
return result >>> 0;
16+
};

0 commit comments

Comments
 (0)