-
Notifications
You must be signed in to change notification settings - Fork 43
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
vi #39
base: master
Are you sure you want to change the base?
vi #39
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 |
---|---|---|
|
@@ -2,20 +2,59 @@ | |
|
||
# This method will return an array of arrays. | ||
# Each subarray will have strings which are anagrams of each other | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def grouped_anagrams(strings) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
end | ||
anagram_hash = Hash.new | ||
|
||
strings.each do |word| | ||
sorted_string = word.chars.sort.join | ||
|
||
#Check if key exists with the sorted string already | ||
#In case of yes, add the word to the array value of the key | ||
if anagram_hash[sorted_string] | ||
anagram_hash[sorted_string] += [word] | ||
|
||
#If not, set it as a new key/value pair in the hash | ||
else | ||
anagram_hash[sorted_string] = [word] | ||
end | ||
end | ||
return anagram_hash.valuesend | ||
|
||
# This method will return the k most common elements | ||
# in the case of a tie it will select the first occuring element. | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def top_k_frequent_elements(list, k) | ||
Comment on lines
+28
to
30
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. This works, but take a look at my comments on time complexity. |
||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
end | ||
result = [] | ||
|
||
hash = Hash.new | ||
|
||
list.each do |letter| | ||
if hash[letter] | ||
hash[letter] += 1 | ||
else | ||
hash[letter] = 1 | ||
end | ||
end | ||
|
||
k.times do | ||
max = nil | ||
hash.each do |num, value| | ||
Comment on lines
+43
to
+45
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. Just note that this iterates k times and each time loops through an hash of n size so this is O(nk) |
||
|
||
if value && (max.nil? || value > max) | ||
max = num | ||
end | ||
end | ||
|
||
if max | ||
hash[max] = nil | ||
result << max | ||
end | ||
end | ||
return result end | ||
|
||
|
||
# This method will return the true if the table is still | ||
|
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.
Missing an
end
after the return