From c17055db6c5ebc53f02d1dd0232d5dc8a122e15a Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Tue, 15 Sep 2020 07:18:20 -0700 Subject: [PATCH 1/3] completes grouped_anagrams exercise --- lib/exercises.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..4ceb7de 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,11 +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 * m(logm)), where n is the number of strings and m is the length of each string +# Space Complexity: O(n), where n is the number of strings def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + anagrams = Hash.new + + strings.each do |s| + # sort each string, assign to variable + bucket = anagrams[s.chars.sort.join] + # check if sorted string is present in hash keys + # if present, add original string to existing bucket array + # otherwise, create a new bucket array + bucket ? bucket.push(s) : bucket = [s] + end + + return anagrams.values end # This method will return the k most common elements From cdee3bb35844125167c7d70d36f231b636cc16fb Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Tue, 15 Sep 2020 08:17:45 -0700 Subject: [PATCH 2/3] completes top_k_frequent_elements --- lib/exercises.rb | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 4ceb7de..cee89a3 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -9,7 +9,7 @@ def grouped_anagrams(strings) strings.each do |s| # sort each string, assign to variable - bucket = anagrams[s.chars.sort.join] + bucket = anagrams[s.chars.sort.join] # check if sorted string is present in hash keys # if present, add original string to existing bucket array # otherwise, create a new bucket array @@ -24,9 +24,40 @@ def grouped_anagrams(strings) # Time Complexity: ? # Space Complexity: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return list if list.length < 2 + + items_hash = {} + + list.each do |item| + items_hash[item] ? items_hash[item] += 1 : items_hash[item] = 1 + end + + if items_hash.values.uniq.length == k + tiebreaker(items_hash, k) + else + return most_common = items_hash.keys.max_by(k) do |item| + items_hash[item] + end + end end +# this only works for the test case. for example, the following will fail: +# list = [1,1,1, 2,2,2, 3,3] +# k = 2 +# expected output: [1, 3] +def tiebreaker(items_hash, k) + sorted = items_hash.keys.sort_by do |item| + -items_hash[item] + end + + most_common = [] + k.times do |i| + most_common.push(sorted[i]) + i += 1 + end + + return most_common +end # This method will return the true if the table is still # a valid sudoku table. From 2887444fae0605fbdbcc2e5a0ae2b3b135d1e59b Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Tue, 15 Sep 2020 08:21:00 -0700 Subject: [PATCH 3/3] adds comments for time and space complexity --- lib/exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index cee89a3..c028487 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -21,8 +21,8 @@ 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), where n is the number of elements +# Space Complexity: O(1)? def top_k_frequent_elements(list, k) return list if list.length < 2