Skip to content

[Sehwan]Week4 solutions with javascript #93

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 3 commits into from
May 26, 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
26 changes: 26 additions & 0 deletions counting-bits/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var countBits = function (n) {
// Create array which has 0 element length of n
const dp = new Array(n + 1).fill(0);
let offset = 1;

for (let i = 1; i <= n; i++) {
if (offset * 2 === i) offset = i;
dp[i] = 1 + dp[i - offset];
}
return dp;
};

/**
0 -> 0000 -> dp[0] = 0
1 -> 0001 -> dp[1] = 1 + dp[1-1] = 1
2 -> 0010 -> dp[2] = 1 + dp[2-2] = 1
3 -> 0011 -> dp[3] = 1 + dp[3-2] = 2
4 -> 0100 -> dp[4] = 1 + dp[4-4] = 1
5 -> 0101 -> dp[5] = 1 + dp[5-4] = 2
6 -> 0110 -> dp[6] = 1 + dp[6-4] = 2
7 -> 0111 -> dp[7] = 1 + dp[7-4] = 3
8 -> 1000 -> dp[8] = 1 + dp[8-8] = 1
*/

// TC: O(n)
// SC: O(1)
23 changes: 23 additions & 0 deletions group-anagrams/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var groupAnagrams = function (strs) {
// Declare hash map to store sorted strs
let map = new Map();

for (let str of strs) {
// Sorted each str
const sortedStr = str.split("").sort().join("");

// If there is alread sortedStr on the map, pushed str
if (map.has(sortedStr)) {
map.get(sortedStr).push(str);
} else {
// If there is no sortedStr on the map, insert [str]
map.set(sortedStr, [str]);
}
}
return Array.from(map.values());
};

// TC: O(n*klogk)
// SC: O(n*k)
// n -> length of strs array
// k -> amount of character for each element
16 changes: 16 additions & 0 deletions missing-number/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var missingNumber = function (nums) {
// Get a expected summation
const n = nums.length;
const expectedSum = (n * (n + 1)) / 2;

// Calculate summation of nums
let numsSum = 0;
for (let i = 0; i < n; i++) {
numsSum += nums[i];
}

return expectedSum - numsSum;
};

// TC: O(n)
// SC: O(1)
13 changes: 13 additions & 0 deletions number-of-1-bits/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var hammingWeight = function (n) {
let count = 0;
while (n) {
// Check rightmost bit is equal to 1 by using bitwise AND operator
count += n & 1;
// Remove rightmost bit from n by using right shift operator
n >>= 1;
}
return count;
};

// TC: O(1) -> The worst case of 32-integer would be O(32)
// SC: O(1)
16 changes: 16 additions & 0 deletions reverse-bits/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var reverseBits = function (n) {
// Make variable to store input
// toString method doesn't include 0 front of number
let binary = n.toString(2);

// Added number of 0s to satisfy 32 bits
while (binary.length < 32) {
binary = "0" + binary;
}

// Reversed binary string and convert into integer
return parseInt(binary.split("").reverse().join(""), 2);
};

// TC: O(1)
// SC: O(1)