Skip to content

Commit

Permalink
Merge pull request #85 from sounmind/main
Browse files Browse the repository at this point in the history
Evan, Week 4
  • Loading branch information
leokim0922 authored May 24, 2024
2 parents e56c3bf + 3170494 commit ca71f64
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
25 changes: 25 additions & 0 deletions counting-bits/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number} n
* @return {number[]}
*/
var countBits = function (n) {
const result = new Array(n + 1).fill(0);

for (let i = 1; i <= n; i++) {
/** The number of 1's in i divided by 2, except last bit of i */
const totalOnes = result[i >> 1];
const lastBit = i & 1;

result[i] = totalOnes + lastBit;
}

return result;
};

/**
* Time Complexity: O(n), where n is number input
* Reason: The algorithm processes each number from 1 to n exactly once.
*
* Space Complexity: O(n), where n is number input
* Reason: The algorithm uses an array of size n + 1
*/
35 changes: 35 additions & 0 deletions group-anagrams/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param {string[]} strList
* @return {string[][]}
*/
var groupAnagrams = function (strList) {
const map = new Map();

for (const str of strList) {
const sortedStr = str.split("").sort().join("");

if (!map.has(sortedStr)) {
map.set(sortedStr, []);
}

map.get(sortedStr).push(str);
}

return Array.from(map.values());
};

/**
* Time Complexity: O(n * k log k)
* Reason:
* - n is the number of strings in the input array.
* - k is the maximum length of a string in the input array.
* - Sorting each string takes O(k log k) time.
* - Thus, sorting all n strings takes O(n * k log k) time.
*
* Space Complexity: O(n * k)
* Reason:
* - We use a map to store the grouped anagrams.
* - In the worst case, we might store all n strings in the map.
* - Each string has a maximum length of k.
* - Thus, the space complexity is O(n * k).
*/
26 changes: 26 additions & 0 deletions missing-number/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const expectedSum = (nums.length * (nums.length + 1)) / 2;
const actualSum = nums.reduce((prev, curr) => prev + curr);

return expectedSum - actualSum;
};

/**
* Time Complexity: O(n)
* Reason:
* - Finding the maximum number in the array takes O(n) time.
* - Calculating the target sum takes O(1) time.
* - Iterating through the array to update the target number takes O(n) time.
* - Checking the condition and returning the result takes O(1) time.
* - Therefore, the overall time complexity is O(n).
*
* Space Complexity: O(1)
* Reason:
* - The algorithm uses a constant amount of extra space (variables like maxNumber, hasZero, targetNumber).
* - No additional space proportional to the input size is used.
* - Therefore, the overall space complexity is O(1).
*/
23 changes: 23 additions & 0 deletions number-of-1-bits/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number} n, maximum 32 bit number
* @return {number}
*/
var hammingWeight = function (n) {
return n.toString(2).replace(/0/g, "").length;
};

/**
* Time Complexity: O(1)
* Reason:
* - n.toString(2): Converts the number to a binary string, which has a fixed length of up to 32 bits. This is O(1) because the length is constant.
* - replace(/0/g, ''): Removes all '0's from the binary string. The operation is O(1) since the string length is at most 32 characters.
* - .length: Getting the length of the resulting string is O(1).
* Therefore, the overall time complexity is O(1).
*
* Space Complexity: O(1)
* Reason:
* - n.toString(2): The binary string representation has a fixed maximum length of 32 characters, which requires O(1) space.
* - replace(/0/g, ''): The resulting string after removing '0's has a length of at most 32 characters, so it also requires O(1) space.
* - .length: Retrieving the length of a string does not require additional space.
* Therefore, the overall space complexity is O(1).
*/
22 changes: 22 additions & 0 deletions reverse-bits/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
const binary = n.toString(2).padStart(32, "0");
const reversedBinary = binary.split("").reverse().join("");

return parseInt(reversedBinary, 2);
};

/**
* Time Complexity: O(1)
* Reason:
* - Each step involves operations on fixed-length strings or arrays (maximum length of 32).
* - Thus, the time complexity for each operation is constant, resulting in an overall time complexity of O(1).
*
* Space Complexity: O(1)
* Reason:
* - The algorithm uses a constant amount of space for the binary string, reversed binary string, and intermediate arrays.
* - Each of these has a fixed length of 32, so the overall space complexity is O(1).
*/

0 comments on commit ca71f64

Please sign in to comment.