Skip to content
Open
Show file tree
Hide file tree
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
87 changes: 77 additions & 10 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,96 @@

# 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) * O(mlogm)
# Space Complexity: O(n+m) = 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.

👍 We normally write the time complexity O(n + m log m)

raise NotImplementedError, "Method hasn't been implemented yet!"

hash_table = Hash.new([])
strings.each do |word| # time: O(n)
hash_table[word.split("").sort.join("")] += [word] # time: O(mlogm), space: o(m) for split, o(n) for hash
end

return hash_table.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(nlogn)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +19 to 21

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!"
end

hash_table = Hash.new(0)

list.each do |int| # time: o(n)
hash_table[int] += 1 # space: o(n)
end

# sort by the values
sorted = hash_table.sort_by{|k, v| -v} # time: o(nlogn), space: o(n)

# return top k
top_k = [] # space: o(m)
sorted[0..k-1].each do |pair| # time: o(m), space: o(m) for []
top_k.push(pair[0]) # time: o(1)
end

return top_k.sort # time: o(mlogm)
end

# This method will return the true if the table is still
# a valid sudoku table.
# Each element can either be a ".", or a digit 1-9
# The same digit cannot appear twice or more in the same
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n^2) - where n and n are rows and cols
# Space Complexity: O(n) - bc hash table created for every row/col separately
def valid_sudoku(table)
Comment on lines +46 to 48

Choose a reason for hiding this comment

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

👍 Nice work

raise NotImplementedError, "Method hasn't been implemented yet!"
# check cols
9.times do |col|
hash_table = {}
table.each_index do |row|
val = table[row][col]
if val != "."
return false if hash_table[val]
hash_table[val] = true
end
end
end

# check rows
table.each_index do |row|
hash_table = {}
table[row].each_index do |col|
val = table[row][col]
if val != "."
return false if hash_table[val]
hash_table[val] = true
end
end
end

# check sub-matrices
i = j = 0
until i == 9 || j == 9
return false unless matrix_helper(table, i, j)
i += 3
j += 3
end

return true
end

def matrix_helper(matrix, i, j)

Choose a reason for hiding this comment

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

I like the helper function here.

hash_table = {}
3.times do |row|
3.times do|col|
val = matrix[row + i][col + j]
if val != "."
return false if hash_table[val]
hash_table[val] = true
end
end
end
return true
end
2 changes: 1 addition & 1 deletion test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
end
end

xdescribe "valid sudoku" do
describe "valid sudoku" do
it "works for the table given in the README" do
# Arrange
table = [
Expand Down