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

Cabebe Spruce C16 #68

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
45 changes: 39 additions & 6 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,51 @@
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 = {}
if not strings:
return []

for string in strings:
letters_in_string = ''.join(sorted(string))
if letters_in_string in anagrams_map:
anagrams_map[letters_in_string].append(string)
else:
anagrams_map[letters_in_string] = [string]

anagrams = []
for item in anagrams_map.values():
anagrams.append(item)

return anagrams



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.

The call to max on line 43 will take O(m) time where m is the number of different frequency counts of the list (so we can put an upper bound of that at O(n)). max will be called up to k times, so the time complexity is actually O(k*n).

Space Complexity: O(n)
"""
pass
element_map = {}
for num in nums:
if num not in element_map:
element_map[num] = 1
else:
element_map[num] += 1

most_common = []
value_list = list(element_map.values())
for key, value in element_map.items():
if value == max(value_list):
most_common.append(key)
value_list.remove(value)
if len(most_common) == k:
break

return most_common


def valid_sudoku(table):
Expand Down