Skip to content

Sockets - Erica#26

Open
norrise120 wants to merge 1 commit intoAda-C11:masterfrom
norrise120:master
Open

Sockets - Erica#26
norrise120 wants to merge 1 commit intoAda-C11:masterfrom
norrise120:master

Conversation

@norrise120
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@eric-andeen eric-andeen left a comment

Choose a reason for hiding this comment

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

Good code.

What other solutions did you consider? This is the brute-force way of solving the problem, and it works, but time complexity is less than ideal. Can you do better without sacrificing space complexity? You should at least discuss in your comments, even if you don't code another solution.

Comment thread lib/array_intersection.rb
# Space complexity: O(n), where n is less than m
def intersection(array1, array2)
raise NotImplementedError
return [] if array1 == nil || array2 == nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nil might be more appropriate if one of the input arrays is nil. The problem description doesn't specify, so your choice is still valid.

Comment thread lib/array_intersection.rb
return [] if array1 == nil || array2 == nil

i = 0
inter_array = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not a great variable name. Choose a name that tells the reader what it's for.

Comment thread lib/array_intersection.rb
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n*m), where n is the number of elements in array1 and m is the number of elements in array2
# Space complexity: O(n), where n is less than m
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Except n is not necessarily less than m. A more accurate statement would be: O(min(n,m)). Also, not that that's the worst case; when one array is a subset of the other. The typical case will be smaller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants