Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown

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 strings are 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 in strings, which is unbounded and could be in the hundreds or thousands.

With that simplifying assumption, we can ignore the k part of this and just say it is O(n).

Space Complexity: O(log(n))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 result for every element of strings, which means there will be O(n) key-value pairs added, giving us a space complexity of O(n).

"""
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 values() by wrapping this in the list() function.

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)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 loop on line 39.

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):
Expand All @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 cols, rows, and squares (9 keys for each column/row/subgrid with up to 9 values each).

Regardless, this is an optional problem.

# """
from collections import defaultdict
def validSudoku(board):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 validSudoku in camel-case instead of valid_sudoku as our automated tests expect, the tests are failing.

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