From d5ef5ca73fd439a73a57ab0d9807bef5250bbe1a Mon Sep 17 00:00:00 2001 From: g4ld Date: Fri, 14 May 2021 21:33:08 -0700 Subject: [PATCH 1/4] updated grouped anagrams --- lib/exercises.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..19b1faa 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -5,7 +5,18 @@ # Space Complexity: ? def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + hash = {} + return [] if strings.nil? || strings.empty? + + strings.each do |string| + key = string.chars.sort.join + if hash.include?(key) + hash[key] << string + else + hash[key] = [string] + end + end + return hash.values end # This method will return the k most common elements From 187597b824d047c97907c4b2e1016364df56716a Mon Sep 17 00:00:00 2001 From: g4ld Date: Fri, 14 May 2021 21:49:19 -0700 Subject: [PATCH 2/4] updated top_k and added space/time complex --- lib/exercises.rb | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 19b1faa..e766445 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) +# Space Complexity: o(n) def grouped_anagrams(strings) hash = {} @@ -21,10 +21,34 @@ 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) +# Space Complexity: o(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + count_hash = {} + answer = [] + return answer if list.nil? || list.empty? + + list.each do |element| + if count_hash?(element) + count_hash[element] += 1 + else + count_hash[element] = 1 + end + end + k.times do + top = nil + count_hash.each do |element,value| + if value && (top.nil? || value > top) + top = element + end + end + if top + count_hash[top] = nil + answer << top + end + end + return answer + end From e5eec0919b3b076444e948fbfa295996964b94a0 Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 17 May 2021 17:50:24 -0700 Subject: [PATCH 3/4] anagram tests passing --- lib/exercises.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e766445..630faca 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -26,7 +26,7 @@ def grouped_anagrams(strings) def top_k_frequent_elements(list, k) count_hash = {} answer = [] - return answer if list.nil? || list.empty? + return answer if list.nil? || list.empty? || !list.is_a?(Array) list.each do |element| if count_hash?(element) From 74cda1ac434fed53417eda94cb508ad08a494b2a Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 17 May 2021 18:09:04 -0700 Subject: [PATCH 4/4] updated k_frequent_elements --- lib/exercises.rb | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 630faca..0884667 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -24,30 +24,24 @@ def grouped_anagrams(strings) # Time Complexity: o(n) # Space Complexity: o(n) def top_k_frequent_elements(list, k) - count_hash = {} - answer = [] - return answer if list.nil? || list.empty? || !list.is_a?(Array) + return [] if list.length == 0 + + hash = {} list.each do |element| - if count_hash?(element) - count_hash[element] += 1 + if hash[element] + hash[element] += 1 else - count_hash[element] = 1 - end - end - k.times do - top = nil - count_hash.each do |element,value| - if value && (top.nil? || value > top) - top = element - end - end - if top - count_hash[top] = nil - answer << top + hash[element] = 1 end - end - return answer + end + + sorted = hash.sort_by {|key, value| -value } + result = [] + k.times do |index| + result << sorted[index].first + end + return result end @@ -55,7 +49,7 @@ def top_k_frequent_elements(list, k) # 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 moregit in the same # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ?