-
Notifications
You must be signed in to change notification settings - Fork 41
Space - Diana #19
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
base: master
Are you sure you want to change the base?
Space - Diana #19
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,52 @@ | ||
|
|
||
| # 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(1) | ||
|
|
||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| string_hashmap = {} | ||
| strings.each do |string| | ||
| if string_hashmap[grouped_anagrams_helper(string)] | ||
| string_hashmap[grouped_anagrams_helper(string)] << string | ||
| else | ||
| string_hashmap[grouped_anagrams_helper(string)] = [] | ||
| string_hashmap[grouped_anagrams_helper(string)] << string | ||
| end | ||
| end | ||
| return string_hashmap.values | ||
| end | ||
|
|
||
| def grouped_anagrams_helper(string) | ||
| return string.chars.sort.join | ||
| 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^2) in worst case because of the sorting to be done | ||
| # Space Complexity: O(1) | ||
| def top_k_frequent_elements(list, k) | ||
|
Comment on lines
+26
to
28
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. You have an O(n log n) time complexity algorithm which is pretty good and O(n) space complexity because you're making a hash and an array both proportional to the size of the original list. |
||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| # build hashmap | ||
| num_hash = {} | ||
| list.each do |num| | ||
| if num_hash[num] | ||
| num_hash[num] += 1 | ||
| else | ||
| num_hash[num] = 1 | ||
| end | ||
| end | ||
|
|
||
| # convert hashmap to array | ||
| array_num_hash = num_hash.to_a | ||
|
|
||
| # sort by occurences | ||
| sorted_array_num_hash = array_num_hash.sort_by {|num| -num[1]} | ||
|
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. just noting this is an O(n log n) operation. |
||
|
|
||
| # sorted elements only | ||
| sorted_numbers = sorted_array_num_hash.map{|num| num[0]} | ||
|
|
||
| # slice sorted array by k elements | ||
| return sorted_numbers.slice(0, k) | ||
| 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.
👍 , however you have O(n) space complexity.