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

Leaves - Mariya #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

In this exercise you will complete two coding problems using Hash Tables.

**Due Date**: Monday Sept 16th

## Learning Goals

By the end of this exercise you should be able to:
Expand Down Expand Up @@ -82,4 +84,4 @@ Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

```
```
39 changes: 33 additions & 6 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,46 @@

# 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)
Comment on lines +5 to 8

Choose a reason for hiding this comment

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

👍 This works and assuming the words are small you are right on the time/space complexity.

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.empty?
anagram_hash = {}

strings.each do |word|
letters = word.split("").sort.join("")
if anagram_hash[letters].nil?
anagram_hash[letters] = [word]
else
anagram_hash[letters] << word
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)
Comment on lines +25 to 27

Choose a reason for hiding this comment

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

This works, but take a look at my comments on big O.

raise NotImplementedError, "Method hasn't been implemented yet!"
counter_hash = {}
list.each do |value|
if counter_hash[value].nil?
counter_hash[value] = 1
else
counter_hash[value] += 1
end
end

values_sort = counter_hash.sort_by {|key, value| -value}

Choose a reason for hiding this comment

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

This sort depends on the number of distinct elements, so it's worst-case O(n log n)

k_values = values_sort.slice(0..(k-1))
final_values = []
k_values.each do |value|
final_values << value[0]
end

return final_values
end


Expand Down
2 changes: 1 addition & 1 deletion test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
end
end

xdescribe "top_k_frequent_elements" do
describe "top_k_frequent_elements" do
it "works with example 1" do
# Arrange
list = [1,1,1,2,2,3]
Expand Down