-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Helena] Week 4 solutions #96
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Time Complexity: O(n) | ||
// Space Complexity: O(n) | ||
|
||
var countBits = function(n) { | ||
// initialize an array to hold the result. | ||
let ans = new Array(n + 1).fill(0); | ||
|
||
// iterate through all numbers from 1 to n. | ||
for (let i = 1; i <= n; i++) { | ||
ans[i] = ans[i >> 1] + (i & 1); | ||
} | ||
return ans; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Time Complexity: O(n * k) | ||
// Space Complexity: O(n * k) | ||
|
||
var groupAnagrams = function(strs) { | ||
const map = new Map(); | ||
for (const str of strs) { | ||
// initialize an array to count the frequency of characters. | ||
const count = new Array(26).fill(0); | ||
// increment the count for each character. | ||
for (const char of str) { | ||
count[char.charCodeAt(0) - 'a'.charCodeAt(0)]++; | ||
} | ||
// generate a unique key based on character frequencies. | ||
const key = count.join('#'); | ||
// if the key exists, push the original string to its group. | ||
if (map.has(key)) { | ||
map.get(key).push(str); | ||
} else { // else, create a new group with the key. | ||
map.set(key, [str]); | ||
} | ||
} | ||
|
||
// return the groups of the map as an array. | ||
return Array.from(map.values()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Time Complexity: O(n) | ||
// Space Complexity: O(1) | ||
|
||
var missingNumber = function(nums) { | ||
const n = nums.length; | ||
// sum of numbers from 0 to n. | ||
const expectedSum = (n * (n + 1)) / 2; | ||
// sum of numbers in the array. | ||
const actualSum = nums.reduce((acc, curr) => acc + curr, 0); | ||
|
||
// the difference is the missing number. | ||
return expectedSum - actualSum; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Time Complexity: O(log n) | ||
// Space Complexity: O(1) | ||
|
||
var hammingWeight = function(n) { | ||
let count = 0; | ||
while (n !== 0) { | ||
// add 1 to count if the last bit is 1. | ||
count += n & 1; | ||
// unsigned right shift to process the next bit. | ||
n = n >>> 1; | ||
} | ||
|
||
return count; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Time Complexity: O(1) | ||
// Space Complexity: O(1) | ||
|
||
var reverseBits = function(n) { | ||
let result = 0; | ||
// iterate 32 times since it's a 32-bit integer. | ||
for (let i = 0; i < 32; i++) { | ||
// shift the result to the left by 1 bit and OR it with the least significant bit of n. | ||
result = (result << 1) | (n & 1); | ||
// right shift n by 1 to consider the next bit. | ||
n >>>= 1; | ||
} | ||
|
||
// convert to unsigned integer. | ||
return result >>> 0; | ||
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.
|
||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ASCII 코드를 활용해서 인덱스 값을 주는 방법도 가능하군요!
좋은 접근방법인 것 같습니다.