Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time - Alicia #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +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: ?
# Time Complexity: O(n^2) - Nested loop
# Space Complexity: O(n)

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
anagram_hash = {}

strings.each do |str|
bucket = anagram_hash.keys.find do |b|
unique_chars = b.chars.sort.join
unique_chars == str.chars.sort.join
end
Comment on lines +11 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using a hash you don't need to do a find here.

Instead consider:

letters = str.chars.sort
if anagram_hash[letters]
  anagram_hash[letters] << str
else
  anagrams_hash[letters] = [str]
end


if !bucket.nil?
anagram_hash[bucket] << str
else
anagram_hash[str] = [str]
end
end
return anagram_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)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
elements_hash = {}

list.each do |element|
if elements_hash[element]
elements_hash[element] += 1
else
elements_hash[element] = 1
end
end

#this doesn't pass tests — instead of grabbing the FIRST most frequent, it grabs the most frequent, period. I couldn't figure out how to do the other way
return elements_hash.keys.max_by(k) { |key| elements_hash[key] }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you can do is find the maximum value in the hash, save the key in an answer array. Then delete that entry from the hash, repeat until you have an answer array that is k length. That would be O(nk) in time complexity.

end


Expand All @@ -25,5 +50,5 @@ def top_k_frequent_elements(list, k)
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
# raise NotImplementedError, "Method hasn't been implemented yet!"
end