-
Notifications
You must be signed in to change notification settings - Fork 76
Spruce - Angela Fan #51
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 |
---|---|---|
|
@@ -2,28 +2,75 @@ | |
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^2) | ||
Space Complexity: O(n) | ||
""" | ||
pass | ||
if not strings: | ||
return [] | ||
anagrams = {} | ||
for string in strings: | ||
sorted_string = "".join(sorted(string)) | ||
if sorted_string in anagrams: | ||
anagrams[sorted_string].append(string) | ||
else: | ||
anagrams[sorted_string] = [string] | ||
return list(anagrams.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) | ||
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 this solution uses |
||
Space Complexity: O(n) | ||
""" | ||
pass | ||
if not nums: | ||
return [] | ||
counts = {} | ||
for num in nums: | ||
if num in counts: | ||
counts[num] += 1 | ||
else: | ||
counts[num] = 1 | ||
sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True) | ||
return [x[0] for x in sorted_counts[:k]] | ||
|
||
def valid_row(row): | ||
if not row: | ||
return False | ||
for i in range(1, 10): | ||
if str(i) not in row: | ||
return False | ||
Comment on lines
+39
to
+41
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. The Sudoku problem is optional, so no need to revisit, but something to consider if you were wondering about the test failures: this function will return A row can be valid without being fully filled in yet, and when |
||
return True | ||
|
||
def valid_subgrid(subgrid): | ||
if not subgrid: | ||
return False | ||
for row in subgrid: | ||
if not valid_row(row): | ||
return False | ||
return True | ||
|
||
def valid_sudoku(table): | ||
""" 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: ? | ||
Time Complexity: O(n^2) | ||
Space Complexity: O(n) | ||
""" | ||
pass | ||
|
||
if not table: | ||
return False | ||
for row in table: | ||
if not valid_row(row): | ||
return False | ||
for col in zip(*table): | ||
if not valid_row(col): | ||
return False | ||
for i in range(0, 9, 3): | ||
for j in range(0, 9, 3): | ||
if not valid_subgrid(table[i:i+3], table[j:j+3]): | ||
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. It doesn't look like the function declaration for |
||
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.
We want to be careful about reusing the same variable for complexity calculations. It seems like
n
is being used as both the number of strings instrings
and the number of letters in a word. I would suggest using something likem
for the number of letters in a word for clarity.Looking at the overall Big O, and line 12 in particular, calling the
sorted
function on a list is an O(m*log(m)) operation. Ifn
is the length of the inputstrings
andm
is the length of a word, our overall time complexity would beO(n*m*log(m))