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
179 changes: 171 additions & 8 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,56 @@

# 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 logn) where n is the length of the strings in the strings element

Choose a reason for hiding this comment

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

I would say that it's O(n) where n is the number of strings if the length of any particular string is pretty small.

It's O(n m log m) where n is the number of strings and m is the average length of any particular string, if the strings can be arbitrarily large.

# Space Complexity: O(n) where n is the length of the input
def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings == []
anagrams = {}

strings.each_with_index do |string, i|
sorted_string = string.split("").sort.join("").downcase
if anagrams[sorted_string]
anagrams[sorted_string] << string
else
anagrams[sorted_string] = [string]
end
end

return anagrams.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) where n is the length of list
# Space Complexity: O(n) where n is the length of the list
def top_k_frequent_elements(list, k)

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!"
return [] if list == []

frequency = {}
list.each do |element|
if frequency[element]
frequency[element] += 1
else
frequency[element] = 1
end
end

k_frequent_elements = []

k.times do |i|
max_freq = 0
max_freq_element = nil
frequency.each do |element, freq|
if freq > max_freq
max_freq = freq
max_freq_element = element
end
end
k_frequent_elements << max_freq_element
frequency[max_freq_element] = 0 if max_freq_element
end

return k_frequent_elements
end


Expand All @@ -26,5 +63,131 @@ def top_k_frequent_elements(list, k)
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
subgrids = {}

# return false if any element is repeated in a row
table.each do |row|
row_count = {}
row.each do |element|
if row_count[element]
return false unless element == "."
else
row_count[element] = 1
end
end
end

# return false if any element is repeated in a column
columns = {
0 => {},
1 => {},
2 => {},
3 => {},
4 => {},
5 => {},
6 => {},
7 => {},
8 => {},
}
table.each do |row|
row.each_with_index do |element, index|
if columns[index][element]
return false unless element == "."
else
columns[index][element] = 1
end
end
end

first_subgrid = {}
second_subgrid = {}
third_subgrid = {}

table[0..2].each do |row|

Choose a reason for hiding this comment

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

This looks pretty repetitive, could you instead create a method to do this on different subgrids?

row[0..2].each do |element|
if first_subgrid[element]
return false unless element == "."
else
first_subgrid[element] = 1
end
end

row[3..5].each do |element|
if second_subgrid[element]
return false unless element == "."
else
second_subgrid[element] = 1
end
end

row[6..8].each do |element|
if third_subgrid[element]
return false unless element == "."
else
third_subgrid[element] = 1
end
end
end

first_subgrid = {}
second_subgrid = {}
third_subgrid = {}

table[3..5].each do |row|
row[0..2].each do |element|
if first_subgrid[element]
return false unless element == "."
else
first_subgrid[element] = 1
end
end

row[3..5].each do |element|
if second_subgrid[element]
return false unless element == "."
else
second_subgrid[element] = 1
end
end

row[6..8].each do |element|
if third_subgrid[element]
return false unless element == "."
else
third_subgrid[element] = 1
end
end
end

first_subgrid = {}
second_subgrid = {}
third_subgrid = {}

table[6..8].each do |row|
row[0..2].each do |element|
if first_subgrid[element]
return false unless element == "."
else
first_subgrid[element] = 1
end
end

row[3..5].each do |element|
if second_subgrid[element]
return false unless element == "."
else
second_subgrid[element] = 1
end
end

row[6..8].each do |element|
if third_subgrid[element]
return false unless element == "."
else
third_subgrid[element] = 1
end
end
end

return true
end
4 changes: 2 additions & 2 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
end
end

xdescribe "top_k_frequent_elements" do
describe "top_k_frequent_elements" do
it "works with example 1" do
# Arrange
list = [1,1,1,2,2,3]
Expand Down Expand Up @@ -131,7 +131,7 @@

end

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