forked from AdaGold/array-intersection
-
Notifications
You must be signed in to change notification settings - Fork 43
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
sjscotton
wants to merge
3
commits into
Ada-C11:master
Choose a base branch
from
sjscotton:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sockets - Sarah #14
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.