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 - Sophie #12

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
25 changes: 23 additions & 2 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,36 @@
# Space Complexity: ?

def grouped_anagrams(strings)

Choose a reason for hiding this comment

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

Space & Time complexity?

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.length == 0
array = strings.group_by { |string| string.chars.sort }.values
return array
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 13 to 17

Choose a reason for hiding this comment

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

👍 Space & time complexity?

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if list.empty?

counter_hash = Hash.new(0)

list.each do |num|
counter_hash[num] += 1
end

sorted_hash = counter_hash.sort_by {|key, value| -value}

array = []

i = 0
while i < k do
array << sorted_hash[i][0]
i += 1
end

return array

end


Expand Down