-
Notifications
You must be signed in to change notification settings - Fork 40
Tatiana #18
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?
Tatiana #18
Changes from all commits
009185f
fae804a
4e7bfa4
715215c
d620505
e5ec94f
a39f1ec
a2de901
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 |
|---|---|---|
|
|
@@ -2,19 +2,57 @@ | |
|
|
||
| # 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) it must go through the entire list | ||
| # Space Complexity: O(n) we return an array of size n same as the input array | ||
|
|
||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| string_hash = {} | ||
|
|
||
| return [] if strings.empty? | ||
|
|
||
| # loop through array, | ||
| strings.each do |word| | ||
| sorted_word = word.chars.sort.join | ||
| if string_hash[sorted_word] | ||
| string_hash[sorted_word].push(word) | ||
| else | ||
| string_hash[sorted_word] = [word] | ||
| end | ||
| end | ||
|
|
||
| return string_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(nLogn) | ||
|
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. where You also should consider that the list might be much longer than the number of distinct elements. So maybe O(n + k log k) where |
||
| # Space Complexity: O(n) | ||
| def top_k_frequent_elements(list, k) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| element_counts = {} | ||
| count = 0 | ||
|
|
||
| return list if list.empty? | ||
|
|
||
| list.each do |num| | ||
| if element_counts[num] | ||
| element_counts[num] += 1 | ||
| else | ||
| element_counts[num] = 1 | ||
| end | ||
|
|
||
| end | ||
|
|
||
| sorted_hash_array = element_counts.sort_by{ |key, val| -val} | ||
|
|
||
| puts sorted_hash_array.to_s | ||
| return_array = [] | ||
| k.times do |i| | ||
| return_array.push(sorted_hash_array[i][0]) | ||
| i += 1 | ||
|
Comment on lines
+50
to
+51
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. indentation |
||
| end | ||
|
|
||
| return return_array | ||
|
|
||
| end | ||
|
|
||
|
|
||
|
|
@@ -27,4 +65,4 @@ def top_k_frequent_elements(list, k) | |
| # Space Complexity: ? | ||
| def valid_sudoku(table) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| end | ||
| 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.
👍