diff --git a/counting-bits/nhistory.js b/counting-bits/nhistory.js new file mode 100644 index 000000000..83e70952e --- /dev/null +++ b/counting-bits/nhistory.js @@ -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) diff --git a/group-anagrams/nhistory.js b/group-anagrams/nhistory.js new file mode 100644 index 000000000..b72bcff0d --- /dev/null +++ b/group-anagrams/nhistory.js @@ -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 diff --git a/missing-number/nhistory.js b/missing-number/nhistory.js new file mode 100644 index 000000000..2d25cdf9e --- /dev/null +++ b/missing-number/nhistory.js @@ -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) diff --git a/number-of-1-bits/nhistory.js b/number-of-1-bits/nhistory.js new file mode 100644 index 000000000..4a14ccfb9 --- /dev/null +++ b/number-of-1-bits/nhistory.js @@ -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) diff --git a/reverse-bits/nhistory.js b/reverse-bits/nhistory.js new file mode 100644 index 000000000..2bf73f1fc --- /dev/null +++ b/reverse-bits/nhistory.js @@ -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)