-
Notifications
You must be signed in to change notification settings - Fork 41
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
Space - Hala #18
base: master
Are you sure you want to change the base?
Space - Hala #18
Conversation
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.
Not bad, you have all the solutions working. You can do things a bit more efficiently using hashes a bit more. This is something to practice a bit as you prepare for interviews.
# Time Complexity: O(n2) | ||
# Space Complexity: O(n) | ||
|
||
def grouped_anagrams(strings) |
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.
👍 This works, but you can do better by sorting the words and using the sorted letters as keys for all the anagrams
|
||
numHash.keys.each do |num| | ||
if returnList.length < k | ||
returnList<< num |
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.
returnList<< num | |
returnList<< num |
numHash = {} | ||
returnList = [] | ||
list.each do |num| | ||
if numHash.keys.include?(num) |
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.
Can be simplified
if numHash.keys.include?(num) | |
unless numHash[num].nil? |
# Time Complexity: O(n2) | ||
# Space Complexity: O(n) | ||
def top_k_frequent_elements(list, k) |
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.
Your solution has 3 loops which each run n times. It also sorts the array. So the method is O(n log n) in time complexity.
Hash Table Practice
Congratulations! You're submitting your assignment!
Comprehension Questions