Skip to content
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

Rachael ---Waterrrrr #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,49 @@
# Space Complexity: ?

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
if strings.length == 0
return []
end
anagrams = {}

strings.each do |string|
sorted = string.chars.sort
if anagrams[sorted]
anagrams[sorted] << string
else
anagrams[sorted] = [string]
end
end

return anagrams.values

end

# 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: ?
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
if list.length == 0
return []
end

counter_hash = {}
list.each do |num|
if counter_hash[num]
counter_hash[num] += 1
else
counter_hash[num] = 1
end
end
result = []
counter_hash = counter_hash.sort_by{|k,v| v }.reverse
index = 0
k.times do
result << counter_hash[index][0]
index += 1
end
return result
end


Expand Down