London | 25-SDC-Nov | Emiliano Uruena | Sprint 1 | Analyse and Refactor Functions#129
London | 25-SDC-Nov | Emiliano Uruena | Sprint 1 | Analyse and Refactor Functions#129Emilianouz wants to merge 6 commits intoCodeYourFuture:mainfrom
Conversation
| * Optimal Time Complexity: | ||
| * Time Complexity: for each element in the input is compared against all previous unique elements: o(n2) | ||
| * Space Complexity: we store n elements it all elements are unique. | ||
| * Optimal Time Complexity: Each element is processed once O(1). |
There was a problem hiding this comment.
Can you elaborate how you derived the optimal time complexity of O(1)?
| let i = 0; | ||
| i < inputSequence.length; | ||
| i++ |
| if (!seenItems.has(value)) { | ||
| seenItems.add(value); | ||
| uniqueItems.push(value); | ||
| } |
There was a problem hiding this comment.
JS Set object preserves the order of which the elements are added to the set. So for better performance we could rely on its built-in ability to convert an array to a set and vice versa.
| Space Complexity: | ||
| space is constant O(1) | ||
| Optimal time complexity: | ||
| With only one loop for both operations reduce complexity to o(1) |
There was a problem hiding this comment.
The optimal time complexity refers to the complexity of the function, and it is not O(1).
| Time Complexity: for each element in first_sequence, is compared against every element in second_sequence. | ||
| Space Complexity: O(the number of unique common elements) | ||
| Optimal time complexity: improved using a set for fast lookups |
There was a problem hiding this comment.
Can you specify both complexities in big-O notations?
Updated the optimal time complexity comment to reflect correct analysis.
Clarified optimal time complexity explanation in comments.
Clarified optimal time and space complexity in docstring.
Updated time and space complexity explanations in docstring.
| } | ||
| Time Complexity: | ||
| There were two loops, one for each operation. Each has complexity O(n) | ||
| for both loops, complexity is O(2n) |
There was a problem hiding this comment.
Note: Constant multiplier is ignored in complexity analysis. O(2n) and O(n) means the same thing.
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity: The time complexity is O(n + m) because we build a set from the second sequence in O(m) time and iterate through the first sequence in O(n) time. |
There was a problem hiding this comment.
This seems like a complexity analysis for the refactored code. The original code does not use any set.
What's the complexity of the original code?
Self checklist
Changelist
Refactor algorithms for improved efficiency and Complexity.