Skip to content

Commit ec1018b

Browse files
authored
Merge pull request #93 from nhistory/week4
[Sehwan]Week4 solutions with javascript
2 parents 3bea8b3 + d9c6ac9 commit ec1018b

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

counting-bits/nhistory.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var countBits = function (n) {
2+
// Create array which has 0 element length of n
3+
const dp = new Array(n + 1).fill(0);
4+
let offset = 1;
5+
6+
for (let i = 1; i <= n; i++) {
7+
if (offset * 2 === i) offset = i;
8+
dp[i] = 1 + dp[i - offset];
9+
}
10+
return dp;
11+
};
12+
13+
/**
14+
0 -> 0000 -> dp[0] = 0
15+
1 -> 0001 -> dp[1] = 1 + dp[1-1] = 1
16+
2 -> 0010 -> dp[2] = 1 + dp[2-2] = 1
17+
3 -> 0011 -> dp[3] = 1 + dp[3-2] = 2
18+
4 -> 0100 -> dp[4] = 1 + dp[4-4] = 1
19+
5 -> 0101 -> dp[5] = 1 + dp[5-4] = 2
20+
6 -> 0110 -> dp[6] = 1 + dp[6-4] = 2
21+
7 -> 0111 -> dp[7] = 1 + dp[7-4] = 3
22+
8 -> 1000 -> dp[8] = 1 + dp[8-8] = 1
23+
*/
24+
25+
// TC: O(n)
26+
// SC: O(1)

group-anagrams/nhistory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var groupAnagrams = function (strs) {
2+
// Declare hash map to store sorted strs
3+
let map = new Map();
4+
5+
for (let str of strs) {
6+
// Sorted each str
7+
const sortedStr = str.split("").sort().join("");
8+
9+
// If there is alread sortedStr on the map, pushed str
10+
if (map.has(sortedStr)) {
11+
map.get(sortedStr).push(str);
12+
} else {
13+
// If there is no sortedStr on the map, insert [str]
14+
map.set(sortedStr, [str]);
15+
}
16+
}
17+
return Array.from(map.values());
18+
};
19+
20+
// TC: O(n*klogk)
21+
// SC: O(n*k)
22+
// n -> length of strs array
23+
// k -> amount of character for each element

missing-number/nhistory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var missingNumber = function (nums) {
2+
// Get a expected summation
3+
const n = nums.length;
4+
const expectedSum = (n * (n + 1)) / 2;
5+
6+
// Calculate summation of nums
7+
let numsSum = 0;
8+
for (let i = 0; i < n; i++) {
9+
numsSum += nums[i];
10+
}
11+
12+
return expectedSum - numsSum;
13+
};
14+
15+
// TC: O(n)
16+
// SC: O(1)

number-of-1-bits/nhistory.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var hammingWeight = function (n) {
2+
let count = 0;
3+
while (n) {
4+
// Check rightmost bit is equal to 1 by using bitwise AND operator
5+
count += n & 1;
6+
// Remove rightmost bit from n by using right shift operator
7+
n >>= 1;
8+
}
9+
return count;
10+
};
11+
12+
// TC: O(1) -> The worst case of 32-integer would be O(32)
13+
// SC: O(1)

reverse-bits/nhistory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var reverseBits = function (n) {
2+
// Make variable to store input
3+
// toString method doesn't include 0 front of number
4+
let binary = n.toString(2);
5+
6+
// Added number of 0s to satisfy 32 bits
7+
while (binary.length < 32) {
8+
binary = "0" + binary;
9+
}
10+
11+
// Reversed binary string and convert into integer
12+
return parseInt(binary.split("").reverse().join(""), 2);
13+
};
14+
15+
// TC: O(1)
16+
// SC: O(1)

0 commit comments

Comments
 (0)