diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..e75e484 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,30 +1,44 @@ - - -# This method will return an array of arrays. -# Each subarray will have strings which are anagrams of each other -# Time Complexity: ? -# Space Complexity: ? - def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + h = {} + solution = [] + strings.each do |word| + chars = word.split('').sort().join() + if !h[chars] + h[chars]= [] + end + h[chars] << word + return solution + end 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: ? def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" -end +counts = {} + list.each do |single| + counts[single] ||= 0 + counts[single] += 1 + 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: ? -def valid_sudoku(table) - raise NotImplementedError, "Method hasn't been implemented yet!" +deltas = {} + + counts.keys.each do |num| + deltas[counts[num]] ||= [] + deltas[counts[num]] << num + end + +remains = k +max = deltas.keys.max +solution = [] + +while remains > 0 && max >=0 + if deltas[max] + deltas[max].each do |num| + solution << num + remains -= 1 + break if remains == 0 + end + end +max -= 1 +end +return solution end \ No newline at end of file