Skip to content

Conversation

@kaseea
Copy link

@kaseea kaseea commented Apr 22, 2019

No description provided.

Copy link

@ace-n ace-n left a comment

Choose a reason for hiding this comment

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

Tests pass and existing complexity analysis LGTM other than variable definitions. 👍

However, I feel like we can probably improve our space complexity (which is what we want, per the README) - even if time complexity suffers as a result. Any ideas on how we'd do that?

# Returns a new array to that contains elements in the intersection of the two input arrays
# Time complexity: ?
# Space complexity: ?
# Time complexity: Time complexity is O(n) (or, O(n+m)) we are looping through one array O(n), then searching worst case for a look up a hash table O(n)
Copy link

Choose a reason for hiding this comment

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

I suspect this is correct - but what is m here? 🙂

(Make sure to define all the letters you use.)

# Time complexity: ?
# Space complexity: ?
# Time complexity: Time complexity is O(n) (or, O(n+m)) we are looping through one array O(n), then searching worst case for a look up a hash table O(n)
# Space complexity: O(n) with n elements in the hash table.
Copy link

Choose a reason for hiding this comment

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

I take it n in the time and space complexities are the same? (It never hurts to be explicit when discussing complexity.)

end

def sorting(longer, shorter)
holder_hash = Hash.new
Copy link

Choose a reason for hiding this comment

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

holder_hash and intersec are extra datastructures that increase space complexity. Can you think of a better approach that doesn't use extra datastructures and has a space complexity of O(1)? (Hint: it may have worse time complexity than your current one.)

holder_hash = Hash.new
longer.each do |num|
holder_hash[num] = 1
#it won't work of I just do holder_hash = Hash.new, should set default value to nil, but it errors on the tests, why?
Copy link

@ace-n ace-n Apr 29, 2019

Choose a reason for hiding this comment

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

Because you have to tell holder_hash what values are in longer. Otherwise, when you loop through shorter holder_hash won't contain anything, and this method will return an empty array.

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