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
33 changes: 26 additions & 7 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@
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(n)
"""
pass
anagrams_map = {}
for word in strings:
alpha_word = "".join(sorted(word))
if anagrams_map.get(alpha_word):
anagrams_map[alpha_word].append(word)
else:
anagrams_map[alpha_word]= [word]


return list(anagrams_map.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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to account for the call to sorted in line 32, which will take O(n * log(n)) time, and thus be the dominant part, and the correct time complexity.

Space Complexity: O(n)
"""
pass
nums_map = {}

for num in nums:
if not nums_map.get(num):
nums_map[num] = 0
nums_map[num] += 1

sorted_nums_map_keys = sorted(nums_map.keys(), key=nums_map.get, reverse = True )
return sorted_nums_map_keys[:k]


def valid_sudoku(table):
Expand All @@ -25,5 +42,7 @@ def valid_sudoku(table):
Time Complexity: ?
Space Complexity: ?
"""
pass
is_valid_sodoku = False

return is_valid_sodoku