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

Time - Yesenia #33

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
60 changes: 54 additions & 6 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@

# 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)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method hasn't been implemented yet!"
hash = {}

strings.each do |word|
letter_grouping = word.split('').sort().join()

if !hash[letter_grouping]
hash[letter_grouping] = []
end

hash[letter_grouping] << word
end

return hash.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: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +25 to 27

Choose a reason for hiding this comment

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

👍 , Nice

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

counts_hash = {}

list.each do |number|
if !counts_hash[number]
counts_hash[number] = 1
else
counts_hash[number] = counts_hash[number] + 1
end
end

length_to_num_hash = {}

counts_hash.keys.each do |number|
if !length_to_num_hash[counts_hash[number]]
length_to_num_hash[counts_hash[number]] = []
end
length_to_num_hash[counts_hash[number]] << number
end

result_length = k
result = []
most_freq_num = length_to_num_hash.keys.max

while result_length > 0 && most_freq_num != nil && most_freq_num >= 0
if length_to_num_hash[most_freq_num]
length_to_num_hash[most_freq_num].each do |num|
result << num
result_length -= 1
break if result_length <= 0
end
end
most_freq_num -= 1
end

return result
end


Expand Down