From a93b91cbbb100c964511c8f4019813e615f0e5e2 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 26 Sep 2019 15:22:40 -0700 Subject: [PATCH 1/6] solved group anagrams --- lib/exercises.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..c7949f3 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -6,7 +6,16 @@ # Space Complexity: ? 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 From 994dce2d9b357a27546151c2730e2f96e436460f Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 26 Sep 2019 23:12:04 -0700 Subject: [PATCH 2/6] added solution for valid sudoku --- lib/exercises.rb | 95 ++++++++++++++++++++++++++++++++++++++---- test/exercises_test.rb | 9 +++- 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index c7949f3..02142f6 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,14 +1,14 @@ - +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) buckets = {} - strings.each do |str| - sorted = str.chars.sort.join + strings.each do |str| + sorted = str.chars.sort.join if buckets[sorted] buckets[sorted] << str else @@ -20,10 +20,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(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 + + if !top_values.include?(el) #O(k) + index = k - 1 + 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 + end + return top_values end @@ -35,5 +54,63 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? 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 = [ From cb727218dd806ff59c6743b27e1a7f35e041889c Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 26 Sep 2019 23:16:52 -0700 Subject: [PATCH 3/6] added big O answers for sudoku checker problem --- lib/exercises.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 02142f6..f4efdee 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -51,8 +51,8 @@ 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) # check if rows valid @@ -90,7 +90,7 @@ def valid_sudoku(table) col_end = 3 while col_end <= 9 - # iterate through each value in box to check for duplicates + # 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| From c679b6dfb376d5d756c93b3e643c0cedd78c3b0b Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Fri, 27 Sep 2019 00:34:57 -0700 Subject: [PATCH 4/6] fixed bug in top k frequent elements --- lib/exercises.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index f4efdee..612fae7 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -28,19 +28,18 @@ def top_k_frequent_elements(list, k) list.each do |el| # O(n) counts[el] += 1 - if !top_values.include?(el) #O(k) - index = k - 1 + index = k - 1 + index -= 1 until top_values[index] == 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 + index -= 1 else break end end - end end return top_values end From f3565979caddb5bbf2ce35b0c4b4c4ecc63b9f7f Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Fri, 27 Sep 2019 00:39:32 -0700 Subject: [PATCH 5/6] formatting --- lib/exercises.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 612fae7..fe5bfc7 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -26,20 +26,19 @@ def top_k_frequent_elements(list, k) counts = Hash.new(0) top_values = Array.new() list.each do |el| # O(n) - counts[el] += 1 - + counts[el] += 1 index = k - 1 index -= 1 until top_values[index] == 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 + 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 From 52da162ade8003603667c1f9989f5e37712681af Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Fri, 27 Sep 2019 00:41:22 -0700 Subject: [PATCH 6/6] fixed bug in top k frequent elements --- lib/exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index fe5bfc7..fa668af 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -28,7 +28,7 @@ def top_k_frequent_elements(list, k) list.each do |el| # O(n) counts[el] += 1 index = k - 1 - index -= 1 until top_values[index] == el if top_values.include?(el) #O(k) + 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]