-
Notifications
You must be signed in to change notification settings - Fork 76
Kristina - Maple #60
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?
Kristina - Maple #60
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 @@ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def create_anagram_key(string): | ||||||||||||||||||||||
| return ''.join(sorted(string)) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def grouped_anagrams(strings): | ||||||||||||||||||||||
| """ 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) | ||||||||||||||||||||||
|
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. Yes! Where n is the length of |
||||||||||||||||||||||
| Space Complexity: O(n) | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| pass | ||||||||||||||||||||||
| grouped_words = {} | ||||||||||||||||||||||
| anagrams = [] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for ele in strings: | ||||||||||||||||||||||
| if grouped_words.get(create_anagram_key(ele)) == None: | ||||||||||||||||||||||
|
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 a minor efficiency thing, if you find yourself calling the same function multiple times with the exact same arguments like you do with |
||||||||||||||||||||||
| grouped_words[create_anagram_key(ele)] = [ele] | ||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| grouped_words[create_anagram_key(ele)].append(ele) | ||||||||||||||||||||||
|
Comment on lines
+16
to
+19
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. Instead of calling your helper function three times, consider calling it just once, at the top of the for loop body.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for k, v in grouped_words.items(): | ||||||||||||||||||||||
| anagrams.append(v) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return anagrams | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def top_k_frequent_elements(nums, k): | ||||||||||||||||||||||
| """ 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) | ||||||||||||||||||||||
|
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. Don't forget to account for the call to |
||||||||||||||||||||||
| Space Complexity: O(n) | ||||||||||||||||||||||
|
Comment on lines
+30
to
+31
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. ✨ |
||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| pass | ||||||||||||||||||||||
| frequency_map = {} | ||||||||||||||||||||||
| results = [] | ||||||||||||||||||||||
| for ele in nums: | ||||||||||||||||||||||
| if ele in frequency_map: | ||||||||||||||||||||||
| frequency_map[ele] += 1 | ||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| frequency_map[ele] = 1 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| values_list = list(frequency_map.values()) | ||||||||||||||||||||||
| values_list.sort(reverse=True) | ||||||||||||||||||||||
| k_values_list = values_list[:k] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for key, value in frequency_map.items(): | ||||||||||||||||||||||
| if value in k_values_list: | ||||||||||||||||||||||
| results.append(key) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return results | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def valid_sudoku(table): | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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'm going to assume that
nis the number of words in the list and that bymyou mean the number of letters in each word. That being said, note that the call tosortedwill actually take O(m * log(m)) time, making this more accurately O(n * m * log(m)).However, we can make a simplifying assumption since we know that the input list is made up of English words. English words tend not to get long, only about 5 letters per word on average, so the effect of
mis going to be dwarfed by the effect ofn, since there can easily be hundreds or thousands of words in the list. With that assumption, we can just say the time complexity is just O(n).