-
Notifications
You must be signed in to change notification settings - Fork 39
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 - Anya #25
base: master
Are you sure you want to change the base?
Earth - Anya #25
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 |
---|---|---|
@@ -1,19 +1,41 @@ | ||
|
||
# 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 * m) n is the length of the array, m is the max length of string within the array. | ||
# Could pull chars method to run first on each element of strings, for O(n) n being the longest string or array. | ||
# Space Complexity: O(n) | ||
|
||
def grouped_anagrams(strings) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
return [] if strings.nil? | ||
return [] if strings.empty? | ||
|
||
counts_hash = Hash.new([]) | ||
strings.each do |string| | ||
counts_hash[string.chars.sort] += [string] | ||
end | ||
return counts_hash.map {|k, v| v} | ||
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) n is list length | ||
# Space Complexity: O(n^2 * k) is that right? | ||
def top_k_frequent_elements(list, k) | ||
Comment on lines
+21
to
23
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. 👍 The time complexity is O(n log n) because you have an O(n) loop (the So the time complexity is O(n + n log n + k) ==> O(n log n) because In the space complexity you're creating a hash O(n), a sorted hash O(n), and |
||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
return [] if list.nil? | ||
return [] if list.empty? | ||
|
||
counts_hash = Hash.new(0) # space = O(n) | ||
list.each do |num| | ||
counts_hash[num] += 1 | ||
end | ||
|
||
sorted_values_2d = counts_hash.sort_by {|k, v| -v} # space = O(n) | ||
|
||
k_most_common_ele = [] # space = O(k) | ||
k.times do |i| | ||
k_most_common_ele << sorted_values_2d[i][0] | ||
end | ||
return k_most_common_ele | ||
end | ||
|
||
|
||
|
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.
👍 But the time complexity is O(n) if the strings are limited in size (so you can ignore the sort because it's limited in size), or O(n * m log m) where m is the length of the strings, if the string is not limited in size.