-
Notifications
You must be signed in to change notification settings - Fork 76
maple - ruiz #58
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?
maple - ruiz #58
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,18 +2,36 @@ | |
| 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) | ||
| Space Complexity: O(1) | ||
| """ | ||
| pass | ||
| anagram_dict = {} | ||
|
|
||
| for string in strings: | ||
| word_sorted = "".join(sorted(string)) | ||
| if word_sorted not in anagram_dict: | ||
| anagram_dict[word_sorted] = [string] | ||
| else: | ||
| anagram_dict[word_sorted] = [string] | ||
|
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. Oof, this causes a pretty bad bug that means only the last string of each anagram group is returned, rather than all the strings in that group. I think you meant to However, the solution is otherwise correct, so I'm only going to mark this Yellow. I will upgrade to Green upon this bug being fixed. |
||
|
|
||
| return list(anagram_dict.values()) | ||
|
|
||
| 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) | ||
| Space Complexity: O(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. Likewise, in this case we add an entry to |
||
| """ | ||
| pass | ||
| most_common = {} | ||
| for num in nums: | ||
| if num in most_common: | ||
|
|
||
| most_common[num] += 1 | ||
| else: | ||
| most_common[num] = 1 | ||
| most_common = sorted(most_common.items(), key=lambda x: x[1], reverse=True) | ||
| return [x[0] for x in most_common[:k]] | ||
|
|
||
|
|
||
|
|
||
| def valid_sudoku(table): | ||
|
|
@@ -22,8 +40,22 @@ def 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: ? | ||
| Time Complexity: O(n2) | ||
|
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 would be correct (even given your current buggy implementation) if we were taking in arbitrary nxn grids, but as we are only taking in 3x3 grids, the run time is constant and thus O(1). |
||
| Space Complexity: O(1) | ||
| """ | ||
| pass | ||
| for row in table: | ||
| for element in row: | ||
| if element != ".": | ||
| if element not in "123456789": | ||
| return False | ||
| else: | ||
| if row.count(element) >1: | ||
| return False | ||
| if [row[i] for i in range(9)].count(element)>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. I know this problem is optional but I just want to note that the list comprehension To get the columns, you'll have to iterate over |
||
| return False | ||
| if [table[i][j] for i in range(3) for j in range(3)].count(element) >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. Likewise, this will always return the first subgrid ((0, 0) to (2, 2)) every iteration, so to fully solve this, you will likely need to more explicitly iterate through all the subgrids rather than using a list comprehension. |
||
| return False | ||
| return True | ||
|
|
||
|
|
||
|
|
||
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.
As we possibly add an entry to
anagram_dictfor each word instrings(in the case no words are anagrams of each other), the space complexity here is O(n).