From 26c842afe6a780a51ef4b090c7b589e50a719b8b Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Sun, 15 Sep 2019 14:02:50 -0700 Subject: [PATCH 1/2] wrote first two methods to make tests pass --- lib/exercises.rb | 61 +++++++++++++++++++++++++++++++++++------- test/exercises_test.rb | 2 +- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..92ef388 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -2,29 +2,72 @@ # 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 * mlogm) where n is the length of the strings array and m is the length of each word +# since each word is getting sorted +# Space Complexity: O(n) where n is the size of the strings array - worst case there are no anagrams and the +# hash is the same size as the array def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings.length == 0 + + hash = {} + + strings.each do |word| + sorted_word = word.chars.sort.join + + if !hash[sorted_word] + hash[sorted_word] = [word] + else + hash[sorted_word] << word + end + end + + return hash.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) where n is the length of the list +# Space Complexity: O(n) where n is the length of the lists def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" -end + return [] if list.length == 0 + + hash = {} + list.each do |ele| + if !hash[ele] + hash[ele] = 1 + else + hash[ele] += 1 + end + end + + top_occurring = [] + + k.times do |i| + top_val = nil + top_freq = 0 + hash.each do |ele, freq| + if freq > top_freq + top_freq = freq + top_val = ele + end + end + + hash.delete(top_val) + top_occurring.push(top_val) + end + + return top_occurring +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 +# The same digit cannot appear twice or more in the same # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) raise NotImplementedError, "Method hasn't been implemented yet!" -end \ No newline at end of file +end diff --git a/test/exercises_test.rb b/test/exercises_test.rb index a649110..dd93104 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 8dbf80a3f52891d56078e1dd607430e4ca0b384c Mon Sep 17 00:00:00 2001 From: Alexandria McCarthy Date: Thu, 19 Sep 2019 13:35:08 -0700 Subject: [PATCH 2/2] fixed time complexity --- lib/exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 92ef388..303ffa1 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -27,7 +27,7 @@ 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: O(n) where n is the length of the list +# Time Complexity: O(n * k) where n is the length of the list # Space Complexity: O(n) where n is the length of the lists def top_k_frequent_elements(list, k) return [] if list.length == 0