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
80 changes: 73 additions & 7 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@

# 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 * klog(k))
# Space Complexity: O(n)

def grouped_anagrams(strings)
Comment on lines +4 to 7

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method hasn't been implemented yet!"
hash_map = {}

strings.each do |str| #n
chars = str.chars #k length of string
sorted = chars.sort # k log(k)
hash_map[sorted] ? hash_map[sorted].push(str) : hash_map[sorted] = [].push(str)
end

return hash_map.values
end

# 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)
def top_k_frequent_elements(list, k)
Comment on lines +21 to 23

Choose a reason for hiding this comment

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

👍 It's O(n) due to the time complexity of the max method.

raise NotImplementedError, "Method hasn't been implemented yet!"
k_freq = []
return k_freq if list.empty?

hash_map = Hash.new

list.each do |element|
hash_map[element] ? hash_map[element] += 1 : hash_map[element] = 1
end

vals = hash_map.values
max_vals = vals.max(k)

max_vals.each do |val|
key = hash_map.key(val)
k_freq << key
hash_map.delete(key)
end
return k_freq
end


Expand All @@ -25,5 +50,46 @@ def top_k_frequent_elements(list, k)
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)

Choose a reason for hiding this comment

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

Looks like you're 1/2 way there!

raise NotImplementedError, "Method hasn't been implemented yet!"
table.each do |row|
row_map = {}
row.each do |elem|
if elem != '.'
if row_map[elem]
return false
else
row_map[elem] = 1
end
end
end
end

i = 0
9.times do
column_map = {}
table.each do |row|
col = row[i]
if col != '.'
if column_map[col]
return false
else
column_map[col] = 1
end
end

end
k += 1
end

i = 0
k = 0
9.times do
box_map = {}
while i < i + 3
while k < k + 3

end
end
end

return true
end