-
Notifications
You must be signed in to change notification settings - Fork 43
Ports - Kasey #20
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
base: master
Are you sure you want to change the base?
Ports - Kasey #20
Conversation
ace-n
left a comment
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.
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) |
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.
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. |
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.
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 |
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.
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? |
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.
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.
No description provided.