diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..fa668af 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -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) 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 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 @@ -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 + + # 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 \ No newline at end of file diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..c2e1516 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -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 @@ -82,6 +83,7 @@ end it "works with example 2" do + # Arrange list = [1] k = 1 @@ -94,6 +96,7 @@ end it "will return [] for an empty array" do + # Arrange list = [] k = 1 @@ -106,6 +109,7 @@ end it "will work for an array with k elements all unique" do + # Arrange list = [1, 2, 3] k = 3 @@ -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 @@ -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 = [