Open
Conversation
CheezItMan
reviewed
Sep 28, 2020
CheezItMan
left a comment
There was a problem hiding this comment.
Not bad Alicia, you hit most of the learning goals and got most of the top_k_frequent_elements. Take a look at my comments and see if that would help you get to a solution, or if it gives you follow up questions to ask me.
Also take a look at my comments for the anagrams problem.
Comment on lines
+11
to
+14
| bucket = anagram_hash.keys.find do |b| | ||
| unique_chars = b.chars.sort.join | ||
| unique_chars == str.chars.sort.join | ||
| end |
There was a problem hiding this comment.
Since you're using a hash you don't need to do a find here.
Instead consider:
letters = str.chars.sort
if anagram_hash[letters]
anagram_hash[letters] << str
else
anagrams_hash[letters] = [str]
end| end | ||
|
|
||
| #this doesn't pass tests — instead of grabbing the FIRST most frequent, it grabs the most frequent, period. I couldn't figure out how to do the other way | ||
| return elements_hash.keys.max_by(k) { |key| elements_hash[key] } |
There was a problem hiding this comment.
What you can do is find the maximum value in the hash, save the key in an answer array. Then delete that entry from the hash, repeat until you have an answer array that is k length. That would be O(nk) in time complexity.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hash Table Practice
Congratulations! You're submitting your assignment!
Comprehension Questions