diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..6e98afb0 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -1,21 +1,42 @@ /** * Calculate the sum and product of integers in a list * - * Note: the "sum" is every number added together - * and the "product" is every number multiplied together - * so for example: [2, 3, 5] would return + * For example: [2, 3, 5] returns * { - * "sum": 10, // 2 + 3 + 5 - * "product": 30 // 2 * 3 * 5 + * sum: 10, // 2 + 3 + 5 + * product: 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Numbers to process - * @returns {Object} Object containing running total and product + * @returns {Object} Object containing sum and product */ + +export function calculateSumAndProduct(numbers) { + let sum = 0; + let product = 1; + + for (const num of numbers) { + sum += num; + product *= num; + } + + return { + sum: sum, + product: product, + }; +} + +// Time complexity is O(n) because we visit each element once. + +// Space complexity is O(1) because we use a fixed number of variables, +// regardless of the length of the input array. + +// Optimal time complexity is O(n) because every element must be examined +// to guarantee the correct sum and product. export function calculateSumAndProduct(numbers) { let sum = 0; for (const num of numbers) { diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..a39b3155 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,17 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n + m) + * Space Complexity: O(n + m) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; + +export function findCommonItems(firstArray, secondArray) { + const firstSet = new Set(firstArray); + const secondSet = new Set(secondArray); + + return [...firstSet.intersection(secondSet)]; +} diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..1503ff0a 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,9 +1,9 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find @@ -19,3 +19,33 @@ export function hasPairWithSum(numbers, target) { } return false; } + +// Time complexity is O(n²) because we use nested loops +// to compare pairs of numbers in the array. + +// Space complexity is O(1) because we only use two variables, +// i and j, regardless of the array size. + +// Optimal time complexity is O(n) because using a Set +// allows us to find a matching pair in a single loop. + +//Improved solution-O(n) +export function hasPairWithSum(numbers, target) { + const seen = new Set(); + + for (const num of numbers) { + const needed = target - num; + + if (seen.has(needed)) { + return true; + } + + seen.add(num); + } + + return false; +} + +// Time complexity: O(n) +// Space complexity: O(n) +// Optimal time complexity: O(n) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..07039860 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,9 +1,10 @@ /** - * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. + * 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: O(n²) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n) * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed @@ -17,6 +18,7 @@ export function removeDuplicates(inputSequence) { currentIndex++ ) { let isDuplicate = false; + for ( let compareIndex = 0; compareIndex < uniqueItems.length; @@ -27,6 +29,7 @@ export function removeDuplicates(inputSequence) { break; } } + if (!isDuplicate) { uniqueItems.push(inputSequence[currentIndex]); } @@ -34,3 +37,22 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } + +// Time complexity is O(n²) because we use nested loops +// to compare each item with the unique items already found. + +// Space complexity is O(n) because we create a new array +// that may store up to n unique items. + +// Optimal time complexity is O(n) because using a Set +// allows us to check for duplicates more efficiently. + +export function removeDuplicates(inputSequence) { + const uniqueItems = new Set(inputSequence); + + return [...uniqueItems]; +} + +// Time complexity: O(n) on average +// Space complexity: O(n) +// Optimal time complexity: O(n) 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 cfd5cfdf..2d0dfd19 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 @@ -1,31 +1,19 @@ from typing import Dict, List - def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: - """ - Calculate the sum and product of integers in a list. - Note: the sum is every number added together - and the product is every number multiplied together - so for example: [2, 3, 5] would return - { - "sum": 10, // 2 + 3 + 5 - "product": 30 // 2 * 3 * 5 - } - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - # Edge case: empty list - if not input_numbers: - return {"sum": 0, "product": 1} + total_sum = 0 + total_product = 1 + + for number in input_numbers: + total_sum += number + total_product *= number - sum = 0 - for current_number in input_numbers: - sum += current_number + return {"sum": total_sum, "product": total_product} - product = 1 - for current_number in input_numbers: - product *= current_number - return {"sum": sum, "product": product} +""" +Time Complexity: O(n) +Space Complexity: O(1) +Optimal Time Complexity: O(n) +""" 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 478e2efc..4fac72c9 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -2,20 +2,27 @@ ItemType = TypeVar("ItemType") - def find_common_items( - first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] + first_sequence: Sequence[ItemType], + second_sequence: Sequence[ItemType] ) -> List[ItemType]: - """ - Find common items between two arrays. - - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - 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) + + second_set = set(second_sequence) + common_items = [] + seen = set() + + for item in first_sequence: + if item in second_set and item not in seen: + common_items.append(item) + seen.add(item) + return common_items + + +""" +Find common items between two arrays. + +Time Complexity: O(n * m * k) +Space Complexity: O(k) +Optimal Time Complexity: O(n + m) +""" \ No newline at end of file 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 fe2da517..cdbd689d 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 @@ -1,18 +1,22 @@ -from typing import List, TypeVar +def has_pair_with_sum(numbers, target_sum): -Number = TypeVar("Number", int, float) + seen = set() + for number in numbers: + complement = target_sum - number -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. + if complement in seen: + return True + + seen.add(number) - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True return False + + +""" +Find if there is a pair of numbers that sum to a target value. + +Time Complexity: O(n²) +Space Complexity: O(1) +Optimal Time Complexity: O(n) +""" \ No newline at end of file diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe80..9d69f2f9 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -1,25 +1,21 @@ -from typing import List, Sequence, TypeVar +def remove_duplicates(values): -ItemType = TypeVar("ItemType") - - -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: - """ unique_items = [] + seen = set() 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: unique_items.append(value) + seen.add(value) return unique_items + + +""" +Remove duplicate values from a sequence, +preserving the order of the first occurrence. + +Time complexity: O(n²) +Space complexity: O(n) +Optimal time complexity: O(n) +"""