From 38ddb36a40a36a3db4ca8731f063346a7f23a36f Mon Sep 17 00:00:00 2001 From: pathywang Date: Fri, 25 Sep 2026 21:27:57 +0100 Subject: [PATCH] complete --- .../calculateSumAndProduct.js | 13 ++++++-- .../findCommonItems/findCommonItems.js | 21 +++++++++++-- .../hasPairWithSum/hasPairWithSum.js | 31 +++++++++++++++++-- .../removeDuplicates/removeDuplicates.mjs | 14 +++++++-- .../calculate_sum_and_product.py | 7 +++-- .../find_common_items/find_common_items.py | 7 +++-- .../has_pair_with_sum/has_pair_with_sum.py | 7 +++-- .../remove_duplicates/remove_duplicates.py | 7 +++-- 8 files changed, 83 insertions(+), 24 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..13620be 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,9 @@ * "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 @@ -32,3 +32,10 @@ export function calculateSumAndProduct(numbers) { product: product, }; } + +// For two loops, we have visit each element of array in order to get the result +// so time complexity is O (n) which means linear. +// Regarding space complexity, we only get one result +// for sum and product no matter how long the array is.so it is O(1)(constant) +// While time complexity is O(n), optimal time complexity should be O(n) because loop +// has to go each single element in array. diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..1bfedae 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,9 +1,9 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(nm) + * Space Complexity:O(n+m) + * Optimal Time Complexity: 0(n+m) * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare @@ -12,3 +12,18 @@ export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), ]; + +// Suppose that firstArray has n items and secondArray has m items. +// filter() goes through every item in firstArray → O(n) iterations. +// includes() may have to search through the entire secondArray → O(m). +// so n items × m-item search = O(nm) for time complexity +// Due to that the array produced by filter() and the final array created +// by [...new Set(...)]. These all can grow with input size. So space complexity is O(n+m) +// However,we don't necessarily need to search through secondArray from scratch for every +// item. We could turn secondArray into a Set first: +// export const findCommonItems = (firstArray, secondArray) => { +// const secondSet = new Set(secondArray); +// return [...new Set( +// firstArray.filter(item => secondSet.has(item)) +// )]; +// }; which would create Set: O(m) and search n items: O(n) so total size: O(n + m) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..f0c8cd0 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^2) + * 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,28 @@ export function hasPairWithSum(numbers, target) { } return false; } + +// Since function is to loop inside another loop, it is roughly n × n comparisons, so +// time complexity = O(n²) +// Because we do not create another array, object or set that grows with n, space +// complexity should be O(1) +// Optimal means the best complexity we can achieve with a reasonable algorithm for the problem. +// We can use a Set to remember numbers we've already seen for this function. +export function hasPairWithSum(numbers, target) { + const seen = new Set(); + + for (const number of numbers) { + const needed = target - number; + + if (seen.has(needed)) { + return true; + } + + seen.add(number); + } + + return false; +} +// Instead of checking every possible pair, we ask: +// "Have I already seen the number that would make this number equal the target?" +// which makes optimal time complexity O(n) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..0827f04 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,9 +1,9 @@ /** * 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^2) + * Space Complexity:O(n) + * Optimal Time Complexity:O(n) * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed @@ -34,3 +34,11 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } + +// While function shows loop inside another loop(nested loop), time complexity is O(n^2) +// Because uniqueItems is extra storage, space complexity is O(n) +// Again, we can use Set to make another function which makes faster to run +export function removeDuplicates(inputSequence) { + return [...new Set(inputSequence)]; +} +// So optimal time complexity should be 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 cfd5cfd..6ab1157 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,9 +12,10 @@ 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: - Optimal time complexity: + Time Complexity: O(n) + Space Complexity:O(1) + Optimal time complexity:O(n) + which is the same as JavaScript function calculateSumAndProduct(numbers) """ # Edge case: empty list if not input_numbers: 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..f460980 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,9 +9,10 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity:O(nm) + Space Complexity:O(n+m) + Optimal time complexity:O(n+m) + which is the same as JavaScript const findCommonItems = (firstArray, secondArray) """ common_items: List[ItemType] = [] for i in first_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..5a2c1fd 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,9 +7,10 @@ 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: + Time Complexity: O( n^2) + Space Complexity:O(1) + Optimal time complexity:O(n) + which is the same as JavaScript function hasPairWithSum(numbers, target) """ for i in range(len(numbers)): for j in range(i + 1, len(numbers)): diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..3c3676e 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,9 +7,10 @@ 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: + Time complexity: O(n^2) + Space complexity:O(n) + Optimal time complexity:O(n) + which is the similar as JavaScript function removeDuplicates(inputSequence) """ unique_items = []