Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
Expand All @@ -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.
Comment on lines +36 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: While the complexity remains $O(1)$, combining the loops into one could often slightly improve the performance.

21 changes: 18 additions & 3 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
31 changes: 28 additions & 3 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -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<number>} numbers - Array of numbers to search through
* @param {number} target - Target sum to find
Expand All @@ -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)
14 changes: 11 additions & 3 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
7 changes: 4 additions & 3 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
Loading