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 - Jansen #10

Open
wants to merge 1 commit 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
59 changes: 56 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
# Returns a new array to that contains elements in the intersection of the two input arrays
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(m^2) + O(n log m), where n is the length of the shorter array, and m is the length of the longer array
# Space complexity: Linear or O(n) if n < m (where n is the length of the shorter array, and m is the length of the longer array)
def intersection(array1, array2)
raise NotImplementedError
return [] if array1 == nil || array2 == nil

# Figure out which array is longer (so longest will be sorted)
# (If they're the same, array1 will be sorted)

Choose a reason for hiding this comment

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

Great comment here.

if array1.length > array2.length || array1.length == array2.length
longer_sorted_array = array1
shorter_array = array2
else
longer_sorted_array = array2
shorter_array = array1
end

# Sort larger array
i = 1
while i < longer_sorted_array.length
to_insert = longer_sorted_array[i]
j = i

# searching for correct position to insert longer_sorted_array[j]
while j > 0 && longer_sorted_array[j - 1] > to_insert
longer_sorted_array[j] = longer_sorted_array[j - 1]
j -= 1
end

longer_sorted_array[j] = to_insert
i += 1
end

# For each element in smaller array, perform binary search in sorted array

Choose a reason for hiding this comment

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

Nice re-use of your binary search from previous homework. 👍

results = []

shorter_array.each do |num|
low = 0
high = longer_sorted_array.length - 1

while low < high
mid = (low + high) / 2
if longer_sorted_array[mid] == num
results << num
break
elsif longer_sorted_array[mid] > num
high = mid - 1
elsif longer_sorted_array[mid] < num
low = mid + 1
end

if longer_sorted_array[low] == num
results << num
break
end
end
end

return results

Choose a reason for hiding this comment

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

Nice work!

end