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 8 commits into
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).
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: | ||
| 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?
Refactor to combine sum and product calculations into a single loop, maintaining O(n) complexity.
Removed redundant complexity explanations in docstring.
| Time Complexity: The time complexity is O(n + m) | ||
| Space Complexity: The space complexity is O(n + m) | ||
| Optimal time complexity: The time complexity is O(n + m) |
There was a problem hiding this comment.
What is the time complexity of the the original code?
Can you explan why your implementation has a space and time complexity of O(n+m)?
Self checklist
Changelist
Refactor algorithms for improved efficiency and Complexity.