-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
Evan, Week 4 #85
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
*/ |
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). | ||
*/ |
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). | ||
*/ |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JS를 모르는 저로써는 꽤나 신기한 코드네요 ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||
*/ |
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). | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What an elegant solution! 🏅