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

Space - Nora #25

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
80 changes: 72 additions & 8 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@

# 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) or O(m * n) where m is the number of words and n is the characters
# Space Complexity: O(n) where n is the number of words in the array

def grouped_anagrams(strings)
Comment on lines +4 to 7

Choose a reason for hiding this comment

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

You are right that it's O(nm) which isn't O(n^2) unless n and m are the same. On the other hand if the words are all relatively small (like English words, you can call this O(n).

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

strings.each do |word|
sorted = word.chars.sort
if hash[sorted]
hash[sorted] << word
else
hash[sorted] = [word]
end
end

return 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: ?
# in the case of a tie it will select the first occurring element.
# Time Complexity: O (n log n) due to sort_by / quick sort - best case. Worst case is O(n^2) if the data is already ordered
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +25 to 28

Choose a reason for hiding this comment

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

👍 , although since Ruby uses MergeSort, this is always O(n log n)

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

list.each do |num|
if hash[num]
hash[num] =+ 1
else
hash[num] = 1
end
end

sorted_hash = hash.sort_by {|key, value| -value}

answer = []

k.times do |i|
answer << sorted_hash[i][0]
end

return answer

end


Expand All @@ -24,6 +57,37 @@ def top_k_frequent_elements(list, k)
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?

def complete_sudoku
return {
1 => 1,
2 => 1,
3 => 1,
4 => 1,
5 => 1,
6 => 1,
7 => 1,
8 => 1,
9 => 1
}
end


def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"

# rows = {}
# cols = {}
# mini_square = {}

# i = 0 # current row
# j = 0 # current column
# until i > 9
# until j > 9
# box = table[i][j];
# j += 1
# end
# i += 1
# end
end