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

Earth - Kal #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
1 & 2 passing
kashenafi committed Jul 1, 2021
commit bdac92c24b7100291479e8ab62c2e5b0b4a8d1e9
39 changes: 36 additions & 3 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@

# This method will return an array of arrays.
# Each subarray will have strings which are anagrams of each other
# Time Complexity: ?
# Space Complexity: ?

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.empty?

groups = {}
strings.each do |string|
sorted = string.chars.sort

if groups[sorted]
groups[sorted] << string
else
groups[sorted] = [string]
end
end

return groups.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)
Comment on lines 23 to 27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 No guesses on time/space complexity?

raise NotImplementedError, "Method hasn't been implemented yet!"

frequency_hash = {}
list.each do |n|
if frequency_hash[n]
frequency_hash[n] += 1
else
frequency_hash[n] = 1
end
end

answer = []
k.times do
highest_freq = 0
frequency_hash.each_value do |value|
highest_freq = value if value > highest_freq
end
most_frequent_ele = frequency_hash.key(highest_freq)
answer << most_frequent_ele if most_frequent_ele
frequency_hash.delete(most_frequent_ele)
end

return answer
end