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
104 changes: 94 additions & 10 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@

require "pry"

# 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 * m log m) where m is the number of strings, and m is the length of a string
# Space Complexity: O(n * m) where n is the number of strings, and m is the length of a string (m space complexity from sort)
Comment on lines +5 to +6

Choose a reason for hiding this comment

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

👍


def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
buckets = {}
strings.each do |str|
sorted = str.chars.sort.join
if buckets[sorted]
buckets[sorted] << str
else
buckets[sorted] = [str]
end
end
return buckets.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 * k), where n is the length of the list and k is the number of common elements to return
# Space Complexity: O(n + k) where n is the length of the list and k is the number of common elements to return

Choose a reason for hiding this comment

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

Since you're building a hash of unique elements I would say that dominates the time complexity.

So O(m) where m is the number of unique elements.

def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
counts = Hash.new(0)
top_values = Array.new()
list.each do |el| # O(n)
counts[el] += 1
index = k - 1
index -= 1 until top_values[index + 1] == el if top_values.include?(el) #O(k)
while index >= 0 #O(k)
if counts[el] > counts[top_values[index]]
temp = top_values[index]
top_values[index] = el
top_values[index + 1] = temp if index + 1 < k
index -= 1
else
break
end
end
end
return top_values
end


Expand All @@ -23,8 +49,66 @@ def top_k_frequent_elements(list, k)
# 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 is the number of elements in a row, column, or box
# Space Complexity: O(n) since only one counts hash is used at a time
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"

# check if rows valid
table.each do |row|
counts = Hash.new(0)
row.each do |char|
if char.to_i.to_s == char
counts[char] += 1
return false if counts[char] > 1
end
end
end

# check if columns valid
column = 0
while column < 9
counts = Hash.new(0)
table.each do |row|
char = row[column]
if char.to_i.to_s == char
counts[char] += 1
return false if counts[char] > 1
end
end
column += 1
end

# check if boxes valid

row_start = 0
row_end = 3

while row_end <= 9
col_start = 0
col_end = 3
while col_end <= 9

# iterate through each value in box to check for duplicates
counts = Hash.new(0)
(row_start...row_end).each do |row_index|
(col_start...col_end).each do |col_index|
char = table[row_index][col_index]
if char.to_i.to_s == char
counts[char] += 1
return false if counts[char] > 1
end
end
end
Comment on lines +92 to +101

Choose a reason for hiding this comment

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

Nice, although you might be able to reduce the complexity of this loop a bit by creating a method.


# move on to next box in row
col_start += 3
col_end += 3
end
# move on to next row of boxes
row_start += 3
row_end += 3
end


return true
end
9 changes: 7 additions & 2 deletions test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@
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]
k = 2
Expand All @@ -82,6 +83,7 @@
end

it "works with example 2" do

# Arrange
list = [1]
k = 1
Expand All @@ -94,6 +96,7 @@
end

it "will return [] for an empty array" do

# Arrange
list = []
k = 1
Expand All @@ -106,6 +109,7 @@
end

it "will work for an array with k elements all unique" do

# Arrange
list = [1, 2, 3]
k = 3
Expand All @@ -118,6 +122,7 @@
end

it "will work for an array when k is 1 and several elements appear 1 time (HINT Pick the 1st one)" do

# Arrange
list = [1, 2, 3]
k = 1
Expand All @@ -131,7 +136,7 @@

end

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