-
Notifications
You must be signed in to change notification settings - Fork 40
Heather #15
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?
Heather #15
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 |
|---|---|---|
|
|
@@ -2,29 +2,73 @@ | |
|
|
||
| # 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: Would this by O(nlog n) for sorting of the string? | ||
| # Space Complexity: O(n + m), n is the size of the array and m is the size of the hash | ||
|
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. Since the size of the hash depends on the size of |
||
|
|
||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| if strings == [] | ||
| return [] | ||
| end | ||
|
|
||
| word_hash = {} | ||
|
|
||
| strings.each do |word| | ||
| sorted_word = word.chars.sort.join | ||
| if word_hash[sorted_word] | ||
| word_hash[sorted_word] << word | ||
| else | ||
| word_hash[sorted_word] = [word] | ||
| end | ||
| end | ||
|
|
||
| return word_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(nlog n) because of the sorted array is at least | ||
|
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. True! Could this be faster without sorting? |
||
| # Space Complexity: O(n), which ever takes up more space, the freq_hash (likely this one) or the sorted frequencies | ||
| def top_k_frequent_elements(list, k) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| end | ||
| if list == [] || k == 0 | ||
| return [] | ||
| end | ||
|
|
||
| freq_hash = {} | ||
| unique_values = [] | ||
| max_freq = 0 | ||
| list.each do |num| | ||
| if freq_hash.include?(num) | ||
| freq_hash[num] += 1 | ||
| else | ||
| freq_hash[num] = 1 | ||
| unique_values << num | ||
| end | ||
| end | ||
|
|
||
| sorted_frequencies = freq_hash.values.sort | ||
|
|
||
| answer_arr = [] | ||
| k.times do | ||
|
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. This looks like a loop that runs |
||
| unique_values.each do |num| | ||
| if freq_hash[num] == sorted_frequencies[-1] | ||
| answer_arr << num | ||
| sorted_frequencies.pop | ||
| freq_hash.delete(num) | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return answer_arr | ||
| end | ||
|
|
||
| # This method will return the true if the table is still | ||
| # a valid sudoku table. | ||
| # Each element can either be a ".", or a digit 1-9 | ||
| # The same digit cannot appear twice or more in the same | ||
| # The same digit cannot appear twice or more in the same | ||
| # row, column or 3x3 subgrid | ||
| # Time Complexity: ? | ||
| # 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.
I would say O( n * m log m) where n is the number of words, and m the length of each word. If you can assume that the words are small, then you could say this is O(n)