-
Notifications
You must be signed in to change notification settings - Fork 76
Pine - Ainur #49
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?
Pine - Ainur #49
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,50 @@ | |
| 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(nklog(k)), where k is the length of the string | ||
| Space Complexity: O(log(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. This is incorrect. We still have to add a key-value pair to |
||
| """ | ||
| pass | ||
| if not strings: | ||
| return [] | ||
|
|
||
| result = {} | ||
| for word in strings: | ||
| key = ''.join(sorted(word)) | ||
| if key in result: | ||
| result.get(key).append(word) | ||
| else: | ||
| result[key] = [word] | ||
| return result.values() | ||
|
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. Unfortunately, this does not return a list, which is why the automated tests are failing. You can get a list from a call to I'm a bit of a stickler for the automated tests passing, so I'll have to mark this Yellow for now, but just fix this bug and this part will be good enough for Green. |
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| 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(n) | ||
| """ | ||
| pass | ||
| if not nums: | ||
| return [] | ||
| count = {} | ||
| frequency = [[] for i in range(len(nums)+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. Small style nitpick: I usually prefer to wait to initialize a variable right before it is used, as that reduces the need to scroll back up. So I would put line 33 here down right before the |
||
| for n in nums: | ||
| if n not in count: | ||
| count[n] = 0 | ||
| count[n] += 1 | ||
|
|
||
| for key, val in count.items(): | ||
| frequency[val].append(key) | ||
|
|
||
|
|
||
| result = [] | ||
| for i in range(len(frequency)-1, 0, -1): | ||
| for n in frequency[i]: | ||
| result.append(n) | ||
| if len(result) == k: | ||
| return result | ||
|
|
||
|
|
||
| def valid_sudoku(table): | ||
|
|
@@ -22,8 +54,25 @@ 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: ? | ||
| """ | ||
| pass | ||
| Time Complexity: o(n^2) | ||
| Space 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. I think your time complexity answer would be true if we accepted arbitrary sized grids of nxn, but luckily in this case we only accept 3x3 grids, so we can say this is O(1) for both time and space complexity. However, your space complexity would be off in the nxn in case because we are also using up to n^2 elements in Regardless, this is an optional problem. |
||
| # """ | ||
| from collections import defaultdict | ||
| def validSudoku(board): | ||
|
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 actually looks like a working implementation! However because your function is called This problem is optional, but if you would like to pass the tests when you make that fix to get the anagram tests passing, just change the name here. |
||
| if board == None: | ||
| return None | ||
| N = 9 | ||
| cols = defaultdict(set) | ||
| rows = defaultdict(set) | ||
| squares = defaultdict(set) | ||
|
|
||
| for row in range(9): | ||
| for col in range(9): | ||
| if board[row][col] == ".": | ||
| continue | ||
| if(board[row][col] in rows[row] or board[row][col] in cols[col] or board[row][col] in squares[(row // 3, col // 3)]): | ||
| return False | ||
| cols[col].add(board[row][col]) | ||
| rows[row].add(board[row][col]) | ||
| squares[(row // 3, col // 3)].add(board[row][col]) | ||
| 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.
This is a correct answer, but we can make a simplifying assumption here. Since we know the elements of
stringsare English words, their size is rather constrained. On average words in English have about 5 letters on average, which is going to be quickly dwarfed by the number of strings instrings, which is unbounded and could be in the hundreds or thousands.With that simplifying assumption, we can ignore the
kpart of this and just say it is O(n).