diff --git a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py index cfd5cfd..2b32194 100644 --- a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py +++ b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py @@ -12,8 +12,11 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "sum": 10, // 2 + 3 + 5 "product": 30 // 2 * 3 * 5 } - Time Complexity: - Space Complexity: + Areas of inefficiency in original version: + - Two separate loops over the same list + + Time Complexity: O(n) + Space Complexity: O(1) Optimal time complexity: """ # Edge case: empty list @@ -21,11 +24,10 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: 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} diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index 478e2ef..b21d0d2 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,13 +9,11 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Areas of inefficiency in original version: + - Nested loops -> O(n * m) + + Time Complexity: O(n + m) avrage + Space Complexity: O(n + m) + Optimal time complexity: O(n + m) """ - 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) - return common_items + return list(set(first_sequence) & set(second_sequence)) diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index fe2da51..35a3175 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -7,12 +7,21 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: """ Find if there is a pair of numbers that sum to a target value. - Time Complexity: - Space Complexity: - Optimal time complexity: + Areas of inefficiency in original version: + - Nested loops check all possible pairs, leading to quadratic time + + Time Complexity: O(n) average + Space Complexity: O(n) + Optimal time complexity: O(n) + + - set to track previously seen values. """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True + seen = set() + + for number in numbers: + complement = target_sum - number + if complement in seen: + return True + seen.add(number) + return False diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..4d02cbd 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -6,20 +6,20 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: """ Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. - - Time complexity: - Space complexity: - Optimal time complexity: + + Areas of inefficiency in original version: + - For each value scans the growing list of unique items. + + Time complexity: O(n) average + Space complexity: O(n) + Optimal time complexity: O(n) """ - unique_items = [] + seen = set() + unique_items: List[ItemType] = [] for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: + if value not in seen: + seen.add(value) unique_items.append(value) return unique_items