From b4829e4171ac1b53fc4ab4e9be968b33034a8bce Mon Sep 17 00:00:00 2001 From: OlgaSe Date: Sun, 18 Apr 2021 20:30:41 -0700 Subject: [PATCH 1/3] solved anagram task --- lib/exercises.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..63bdcc5 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -4,8 +4,31 @@ # Time Complexity: ? # Space Complexity: ? +# [eat, meet, tea, ate, hi] + +# { +# 'aet' => [eat, tea, ate], +# 'eemt' => [meet], +# 'ih' => [ih] +# } + def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings.length == 0 + hash = {} + strings.each do |string| + key = anagrams_helper(string) + if !hash[key] + hash[key] = [string] + else + hash[key] << string + end + end + return hash.values +end + +def anagrams_helper(string) + array_of_letters = string.split(//) + return array_of_letters.sort!.join() end # This method will return the k most common elements From b1c629030e1a66e872144665661aa0b15fe0d051 Mon Sep 17 00:00:00 2001 From: OlgaSe Date: Sun, 2 May 2021 18:34:35 -0700 Subject: [PATCH 2/3] added top_k_frequent_solution function --- lib/exercises.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 63bdcc5..64d0e8e 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -36,7 +36,29 @@ def anagrams_helper(string) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if list.length == 0 + hash = {} + result = [] + list.each do |num| + if !hash[num] + hash[num] = 1 + else + hash[num] += 1 + end + end + + sorted_hash = hash.sort_by{|key, value| -value} + + i = 0 + while i < k do + result << sorted_hash[i][0] + i += 1 + end + + # max_pairs = hash.each.max_by(k) { |pair| pair[1] } + # return max_pairs.map {|pair| pair[0]} + + return result end From 4cc6c374fa4c772a59040c7333b64e721a7cfae2 Mon Sep 17 00:00:00 2001 From: OlgaSe Date: Sun, 2 May 2021 19:10:04 -0700 Subject: [PATCH 3/3] added big-o complexity --- lib/exercises.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 64d0e8e..aee085a 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,8 +1,8 @@ # 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 n - length of the strings array, m - length of the longest string +# Space Complexity: O(n) # [eat, meet, tea, ate, hi] @@ -33,8 +33,8 @@ def anagrams_helper(string) # 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) return [] if list.length == 0 hash = {} @@ -54,10 +54,6 @@ def top_k_frequent_elements(list, k) result << sorted_hash[i][0] i += 1 end - - # max_pairs = hash.each.max_by(k) { |pair| pair[1] } - # return max_pairs.map {|pair| pair[0]} - return result end @@ -70,5 +66,6 @@ def top_k_frequent_elements(list, k) # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) + raise NotImplementedError, "Method hasn't been implemented yet!" end