Skip to content

Evan, Week 4 #85

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 5 commits into from
May 24, 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
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;
};
Comment on lines +5 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What an elegant solution! 🏅


/**
* 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;
};
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS를 모르는 저로써는 꽤나 신기한 코드네요 ㅎㅎ
toString(2)가 2진수로 변환하는 거고, replace(/0/g, "")는 Regex로 0을 찾아서 빈 문자열로 변환하는 코드로 이해했는데 맞을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다!
이진수 연산을 도와주는 편한 메서드가 많이 있네요ㅎㅎ


/**
* 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.
Copy link
Member

@DaleSeo DaleSeo May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this built-in function would take a constant amount of time regardless of the input size. 🤔

Copy link
Member Author

@sounmind sounmind May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for feedback☺️
Hmm... I thought this way because input size is fixed with 32 bits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The processing speed will vary depending on the number given, but no matter how large the number gets, 32 bits is the maximum, so it's a constant time in terms of time complexity. Of course, constant time complexity doesn't mean it's efficient.

If you don't think so, can you give me your example?

* - 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).
*/