Skip to content

Conversation

@norrise120
Copy link

No description provided.

Copy link

@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.

# Space complexity: O(n), where n is less than m
def intersection(array1, array2)
raise NotImplementedError
return [] if array1 == nil || array2 == nil

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.

return [] if array1 == nil || array2 == nil

i = 0
inter_array = []

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.

# 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

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