From ead3216bb1ed09a78bd0a080ede73006ea7bf24c Mon Sep 17 00:00:00 2001 From: Kareha Agesa Date: Thu, 15 Apr 2021 20:04:09 -0700 Subject: [PATCH 1/3] grouped anagram tests passing --- lib/exercises.rb | 12 +++++++++--- test/exercises_test.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..01dbf8a 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,11 +1,17 @@ # 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) - 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 diff --git a/test/exercises_test.rb b/test/exercises_test.rb index 74646dc..b205e73 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -68,7 +68,7 @@ end end - describe "top_k_frequent_elements" do + xdescribe "top_k_frequent_elements" do it "works with example 1" do # Arrange list = [1,1,1,2,2,3] From 6d187fcdfc8c7a2fa73fbe3668883b040ddb65be Mon Sep 17 00:00:00 2001 From: Kareha Agesa Date: Thu, 15 Apr 2021 21:26:57 -0700 Subject: [PATCH 2/3] top k frequent elements passing --- lib/exercises.rb | 25 +++++++++++++++++++++---- test/exercises_test.rb | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 01dbf8a..b2cbd8c 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -16,12 +16,29 @@ def grouped_anagrams(strings) # 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) - raise NotImplementedError, "Method hasn't been implemented yet!" -end + # edge cases: k greater than length of string? + + 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. diff --git a/test/exercises_test.rb b/test/exercises_test.rb index b205e73..74646dc 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -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] From 75da4694c1b5cfd9fe7e0271975a720822fb40da Mon Sep 17 00:00:00 2001 From: Kareha Agesa Date: Sun, 18 Apr 2021 22:27:22 -0700 Subject: [PATCH 3/3] valid sudoku passing --- lib/exercises.rb | 56 +++++++++++++++++++++++++++++++++++++----- test/exercises_test.rb | 2 +- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index b2cbd8c..c9d5d59 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -8,7 +8,7 @@ def grouped_anagrams(strings) 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 + hash_table[word.split("").sort.join("")] += [word] # time: O(mlogm), space: o(m) for split, o(n) for hash end return hash_table.values @@ -19,14 +19,12 @@ def grouped_anagrams(strings) # Time Complexity: O(nlogn) # Space Complexity: O(n) def top_k_frequent_elements(list, k) - # edge cases: k greater than length of string? 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) @@ -45,8 +43,54 @@ 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 and n are rows and cols +# Space Complexity: O(n) - bc hash table created for every row/col separately def valid_sudoku(table) - 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) + 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 \ No newline at end of file diff --git a/test/exercises_test.rb b/test/exercises_test.rb index 74646dc..8025075 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -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 = [