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

Sockets - Sarah #14

Open
wants to merge 3 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
63 changes: 60 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
# Returns a new array to that contains elements in the intersection of the two input arrays
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the length of the longer array

Choose a reason for hiding this comment

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

This is actually incorrect. The time complexity would be O(n + m) where n and m are the lengths of the two arrays.

# Space complexity: O(n) where n is the length of shorter array.
# its 2n, because of the space of the hash is linear to the shorter array, and the
# intersection array space is also linear to the smaller array

def intersection(array1, array2)
raise NotImplementedError
return [] if array1 == nil || array2 == nil
if array1.length < array2.length
shorter = array1
longer = array2
else
shorter = array2
longer = array1
end
num_hash = Hash.new
intersection_array = []
shorter.length.times do |index|
num_hash[shorter[index]] = 1
end
longer.length.times do |index|
intersection_array << longer[index] if num_hash[longer[index]]
end

return intersection_array
end

# SORTING SOLUTION

# Returns a new array to that contains elements in the intersection of the two input arrays

Choose a reason for hiding this comment

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

It's nice that you have the sorting solution as well. Nicely done

# Time complexity: O(n log n) where n is the size of the smaller array,
# this is because the highest time complexity is the sruby sort method, which it n log n
# Space complexity: O(n) where n is the length of the smaller array

# def intersection(array1, array2)
# return [] if array1 == nil || array2 == nil
# if array1.length < array2.length
# array1.sort!
# shorter = array1
# longer = array2
# else
# array2.sort!
# shorter = array2
# longer = array1
# end
# new_array = []
# longer.length.times do |index|
# new_array << longer[index] if binary_search(shorter, shorter.length, longer[index])
# end
# return new_array
# end

# def binary_search(array, length, value_to_find)
# high = length
# low = 0
# length.times do
# guess = (low + high) / 2
# return true if array[guess] == value_to_find
# return false if high - low <= 1
# array[guess] < value_to_find ? low = guess : high = guess
# end
# return false
# end