From 6a2267150f7aea7722533c1c302767498657220d Mon Sep 17 00:00:00 2001 From: Kimberly Vitug Date: Sun, 11 Apr 2021 07:59:51 -0700 Subject: [PATCH 1/4] fix README formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b2ef0f..5d6d7ac 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,11 @@ Given a non-empty array of integers, return the *k* most frequent elements. ``` Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] - ``` ## Example 2 +``` Input: nums = [1], k = 1 Output: [1] ``` From b19dac671e88b2b2dbd037d328cbfeab1b9e47d6 Mon Sep 17 00:00:00 2001 From: Kimberly Vitug Date: Sat, 24 Apr 2021 19:00:50 -0700 Subject: [PATCH 2/4] update readme --- README.md | 1 - lib/exercises.rb | 25 +++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5d6d7ac..fc01476 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,6 @@ Output: [1,2] ``` ## Example 2 - ``` Input: nums = [1], k = 1 Output: [1] diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..d2df178 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -5,15 +5,32 @@ # Space Complexity: ? def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + output = Hash.new + + string_letter_count = strings.map do |word| + letter_count = word.each_char.with_object(Hash.new(0)) { |letter, count| count[letter] += 1 } + end + + p string_letter_count[0] == string_letter_count[1] + + # string_letter_count.group_by.with_index { |a, index| output[a] }.values + # p Hash[string_letter_count.zip(strings)].group_by.with_index { |key, i| p key } 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 log n) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + count = Hash.new(0) + + list.each do |num| + count[num] += 1 + end + + count = count.sort_by { |k, v| -v }.to_h + + return count.keys[0...k] end From d71c1f2332c7240ce2429dac9b71b53b87dda209 Mon Sep 17 00:00:00 2001 From: Kimberly Vitug Date: Sat, 24 Apr 2021 19:42:13 -0700 Subject: [PATCH 3/4] add grouped_anagrams solution --- lib/exercises.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index d2df178..321472b 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,20 +1,22 @@ # 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) where n is the number of chars +# Space Complexity: O(n) where n is the number of words def grouped_anagrams(strings) - output = Hash.new + frequency_hash_to_words = Hash.new - string_letter_count = strings.map do |word| - letter_count = word.each_char.with_object(Hash.new(0)) { |letter, count| count[letter] += 1 } - end + strings.each do |word| + frequency_hash = word.each_char.with_object(Hash.new(0)) { |letter, count| count[letter] += 1 } - p string_letter_count[0] == string_letter_count[1] + if frequency_hash_to_words.include?(frequency_hash) + frequency_hash_to_words[frequency_hash] << word + else + frequency_hash_to_words[frequency_hash] = [ word ] + end + end - # string_letter_count.group_by.with_index { |a, index| output[a] }.values - # p Hash[string_letter_count.zip(strings)].group_by.with_index { |key, i| p key } + return frequency_hash_to_words.values end # This method will return the k most common elements From 001fa581531d43ac69fffbd55c7352f2b9792a34 Mon Sep 17 00:00:00 2001 From: Kimberly Vitug Date: Sat, 24 Apr 2021 20:41:07 -0700 Subject: [PATCH 4/4] add sudoku solution --- lib/exercises.rb | 43 +++++++++++++++++++++++++++++++++++++++--- test/exercises_test.rb | 2 +- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 321472b..61d7d26 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -35,14 +35,51 @@ def top_k_frequent_elements(list, k) return count.keys[0...k] end +def get_sub_box(row, col) + return 0 if row <= 2 && col <= 2 + return 1 if row <= 2 && col >= 3 && col <= 5 + return 2 if row <= 2 && col >= 6 + + return 3 if row >= 3 && row <= 5 && col <= 2 + return 4 if row >= 3 && row <= 5 && col >= 3 && col <= 5 + return 5 if row >= 3 && row <= 5 && col >= 6 + + return 6 if row >= 6 && col <= 2 + return 7 if row >= 6 && col >= 3 && col <= 5 + return 8 +end # This method will return the true if the table is still # a valid sudoku table. # 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(1) +# Space Complexity: O(n) def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" + row_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ] + column_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ] + sub_box_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ] + + table.each_with_index do |row, r| + row.each_with_index do |column, c| + cell = table[r][c] + next if cell == '.' + + # check/update row + return false if row_number_frequencies[r].include?(cell) + row_number_frequencies[r][cell] = true + + # check/update column + return false if column_number_frequencies[c].include?(cell) + column_number_frequencies[c][cell] = true + + # check/update sub-box + sub_box = get_sub_box(r, c) + return false if sub_box_number_frequencies[sub_box].include?(cell) + sub_box_number_frequencies[sub_box][cell] = true + end + end + + return true end 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 = [