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

Scissors - Hena #31

Open
wants to merge 1 commit 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
55 changes: 48 additions & 7 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@
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: ?
"""
pass
Time Complexity: O(n)
Space Complexity: O(n)
"""
hash_table = {}
Comment on lines +5 to +8

Choose a reason for hiding this comment

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

👍 Time complexity is correct assuming English words (limited in length).

for word in strings:
# sort each char of word into a list
sorted_word = sorted(word)
# join the sorted chars back into a word
anagram = ''.join(sorted_word)
if anagram in hash_table:
# if anagram is already a key in hash table,
# append the current word to the value
hash_table[anagram].append(word)
else:
# else, assign anagram as a key in the hash table
# and the current word as its value in list form
hash_table[anagram] = [word]
print(hash_table)
# return just the list of all the values
return list(hash_table.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)
Space Complexity: O(n)
"""
Comment on lines +29 to 31

Choose a reason for hiding this comment

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

👍 However because you're sorting, I think the time complexity is O(n log n) worst-case

pass
if not nums:
return nums

hash_table = {}
for num in nums:
if num in hash_table:
hash_table[num] += 1
else:
hash_table[num] = 1

print(hash_table.items())

# didn't grasp 100%, so listed out steps
# grab key-value pairs in hash_table
# let key be the second value in tuple
# reverse sort tuples by value into list
sorted_by_value = sorted(hash_table.items(), key = lambda tuple: tuple[1], reverse=True)
print(sorted_by_value)
result = []
# for first k nums in reversed list of tuples
# append the value, or first value of tuple to result list
for i in range(k):
result.append(sorted_by_value[i][0])
return result


def valid_sudoku(table):
Expand All @@ -24,6 +63,8 @@ def valid_sudoku(table):
row, column or 3x3 subgrid
Time Complexity: ?
Space Complexity: ?

2powerful4me

Choose a reason for hiding this comment

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

Nah, you just don't have time.

"""
pass