-
Notifications
You must be signed in to change notification settings - Fork 76
C16/Cedar- Ana Gabriele #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
base: master
Are you sure you want to change the base?
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,29 +1,115 @@ | ||
|
|
||
| from attr import has | ||
|
|
||
|
|
||
| def grouped_anagrams(strings): | ||
| """ This method will return an array of arrays. | ||
| """ | ||
| - Use a hash table to solve a coding problem | ||
| - Identify how a hash table can produce a more attractive runtime over alternative solutions | ||
| Given an array of strings, group anagrams together. | ||
| ### Example | ||
| ``` | ||
| Input: ["eat", "tea", "tan", "ate", "nat", "bat"], | ||
| Output: | ||
| [ | ||
| ["ate","eat","tea"], | ||
| ["nat","tan"], | ||
| ["bat"] | ||
| ] | ||
| ``` | ||
| Note: | ||
| - All inputs will be in lowercase. | ||
| - The order of your output does not matter | ||
|
|
||
| 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(m * nlogn) - | python sorting algorithm -> O(n. logn). | ||
| Space Complexity: O(n) | ||
| """ | ||
| pass | ||
| # return empty list if no words | ||
| if len(strings) == 0: | ||
| return [] | ||
|
|
||
| hash_table = {} | ||
| for item in strings: | ||
| # sort time complexity => O(n. logn). | ||
| string = "".join(sorted(item)) | ||
| if string not in hash_table: | ||
| hash_table[string] = [item] | ||
| else: | ||
| hash_table[string].append(item) | ||
|
|
||
| print(hash_table.values()) | ||
| return list(hash_table.values()) | ||
|
|
||
|
|
||
| print(grouped_anagrams(["eat", "tae", "tea", "eta", "aet", "ate"])) | ||
|
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. In general, make sure to remove testing code like this before you submit your PRs. |
||
|
|
||
| def top_k_frequent_elements(nums, k): | ||
| """ This method will return the k most common elements | ||
| """ | ||
| ## Most Frequent K Elements | ||
| Given a non-empty array of integers, return the *k* most frequent elements. | ||
| ## Example 1 | ||
| ``` | ||
| Input: nums = [1,1,1,2,2,3], k = 2 | ||
| Output: [1,2] | ||
| ``` | ||
| ## Example 2 | ||
| ``` | ||
| Input: nums = [1], k = 1 | ||
| Output: [1] | ||
| ``` | ||
| ## Note | ||
|
|
||
| You may assume k is always valid, 1 ≤ k ≤ number of unique elements. | ||
|
|
||
| You should be able to equal or beat O(n log n), where n is the array's size. | ||
| 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) | ||
| Space Complexity: O(n) | ||
| """ | ||
| pass | ||
|
|
||
| # if empty list return an empty array | ||
|
|
||
| if len(nums) == 0: | ||
| return [] | ||
| # count the frequency of each element | ||
| hash_table = {} | ||
| frequency = [[] for i in range(len(nums) +1)] | ||
|
|
||
| for item in nums: | ||
| # coutn how many times an item occurs , return 0 if doesn't exists | ||
| hash_table[item] = 1 + hash_table.get(item,0) | ||
| # loop thorugh key-value pair | ||
| for item, count in hash_table.items(): | ||
| # append to the the freq list | ||
| frequency[count].append(item) | ||
|
|
||
| result = [] | ||
|
|
||
| # loop though all the way up until zero | ||
| # range(start, stop, step) | ||
| for index in range(len(frequency) -1, 0, -1): | ||
| for elem in frequency[index]: | ||
| result.append(elem) | ||
| if len(result) == k: | ||
| return result | ||
|
|
||
| print(top_k_frequent_elements([9,9,8,8,7],2)) | ||
|
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. Same thing as above, make sure to remove testing code like this. It helps keep the code cleaner which is good style. |
||
|
|
||
| def valid_sudoku(table): | ||
| """ This method will return the true if the table is still | ||
| """ | ||
| OPTIONAL | ||
| 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 | ||
| row, column or 3x3 subgrid | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| """ | ||
| # Optional QUESTION - | ||
| pass | ||
|
|
||
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.
While this is an accurate answer assuming
nstands for the number of characters in the strings andmstands for the number of strings in the list, we can make a simplifying assumption. Since we know the strings are English words, we can be pretty sure they will not get too big in size. On average words in English have about 5 letters, which is going to very quickly get dwarfed by the number of words in the list so we can simply say this is O(m) in time complexity.