Skip to content
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

Paper--Andrea P #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
43 changes: 36 additions & 7 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,47 @@
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: 0(n)
Space Complexity: O(n)
"""
Comment on lines 2 to 7

Choose a reason for hiding this comment

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

👍 Correct, if the words are of limited length, if the words could be any length then it would be O(n m log m)

pass
if not strings:
return []

map={}
for string in strings:
sorted_string = ''.join(sorted(string))
if sorted_string in map:
map[sorted_string].append(string)
else:
map[sorted_string]=[string]

result=[]
for value in map.values():
result.append(value)
return result


def top_k_frequent_elements(nums, k):
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)
"""
Comment on lines +25 to 30

Choose a reason for hiding this comment

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

👍 Looking at the loop from lines 40-43, the loop runs k times and each time finds the max value of a collection roughly n long. The time complexity is O(nk).

pass
if not nums:
return []

map = {}
for num in nums:
count = map.get(num,0)
map[num]= count +1

result= []
for i in range(k):
k = max(map, key=map.get)
result.append(k)
del map[k]

return result


def valid_sudoku(table):
Expand Down