-
-
Notifications
You must be signed in to change notification settings - Fork 29
London | 25-SDC-Nov | Emiliano Uruena | Sprint 1 | Analyse and Refactor Functions #129
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: main
Are you sure you want to change the base?
Changes from 1 commit
f721493
54c12f0
2805870
e55de56
7ad8c1f
14db9b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,27 @@ | ||
| /** | ||
| * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * 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). | ||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const seenItems = new Set(); | ||
| const uniqueItems = []; | ||
|
|
||
| for ( | ||
| let currentIndex = 0; | ||
| currentIndex < inputSequence.length; | ||
| currentIndex++ | ||
| let i = 0; | ||
| i < inputSequence.length; | ||
| i++ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use a for-of loop? |
||
| ) { | ||
| let isDuplicate = false; | ||
| for ( | ||
| let compareIndex = 0; | ||
| compareIndex < uniqueItems.length; | ||
| compareIndex++ | ||
| ) { | ||
| if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { | ||
| isDuplicate = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!isDuplicate) { | ||
| uniqueItems.push(inputSequence[currentIndex]); | ||
| const value = inputSequence[i]; | ||
|
|
||
| if (!seenItems.has(value)) { | ||
| seenItems.add(value); | ||
| uniqueItems.push(value); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,19 +13,21 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: | |
| "product": 30 // 2 * 3 * 5 | ||
| } | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Constant multiplier is ignored in complexity analysis. O(2n) and O(n) means the same thing. |
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The optimal time complexity refers to the complexity of the function, and it is not O(1). |
||
| """ | ||
| # Edge case: empty list | ||
| if not input_numbers: | ||
| return {"sum": 0, "product": 1} | ||
|
|
||
| sum = 0 | ||
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| product = 1 | ||
| for current_number in input_numbers: | ||
| sum += current_number | ||
| product *= current_number | ||
|
|
||
| return {"sum": sum, "product": product} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,16 @@ def find_common_items( | |
| """ | ||
| Find common items between two arrays. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you specify both complexities in big-O notations? |
||
| """ | ||
| second_set = set(second_sequence) | ||
| seen = set() | ||
| common_items: List[ItemType] = [] | ||
| for i in first_sequence: | ||
| for j in second_sequence: | ||
| if i == j and i not in common_items: | ||
| common_items.append(i) | ||
| if i in second_set and i not in seen: | ||
| seen.add(i) | ||
| common_items.append(i) | ||
|
|
||
| return common_items | ||
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.
Can you elaborate how you derived the optimal time complexity of O(1)?