Skip to content

Commit c060686

Browse files
committed
Complete Sprint 1 complexity exercises
1 parent f13588d commit c060686

4 files changed

Lines changed: 115 additions & 46 deletions

File tree

‎Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,31 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Original code used two separate loops:
13+
* - The first loop calculated the sum.
14+
* - The second loop calculated the product.
15+
*
16+
* I changed the code to use one loop for both calculations.
17+
* This means we do not need to go through the array twice.
18+
*
19+
* Time Complexity: O(n)
20+
* Space Complexity: O(1)
21+
* Optimal Time Complexity: O(n)
22+
*
23+
* Time complexity is O(n) because we need to visit every number.
24+
* Space complexity is O(1) because we use a fixed number of variables.
25+
* O(n) is the best possible time complexity for this task.
1526
*
1627
* @param {Array<number>} numbers - Numbers to process
1728
* @returns {Object} Object containing running total and product
1829
*/
1930
export function calculateSumAndProduct(numbers) {
2031
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2532
let product = 1;
33+
34+
// Calculate the sum and product in the same loop
2635
for (const num of numbers) {
36+
sum += num;
2737
product *= num;
2838
}
2939

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* The original code used includes() to search the second array
5+
* for each item in the first array. This can take a long time
6+
* when the arrays are large.
7+
*
8+
* I changed the code to use a Set for faster searching.
9+
* First, we store the items from the second array in a Set.
10+
* Then, we check which items from the first array are in the Set.
11+
* We use another Set to remove duplicate items from the result.
12+
*
13+
* Time Complexity: O(n + m)
14+
* Space Complexity: O(n + m)
15+
* Optimal Time Complexity: O(n + m)
16+
*
17+
* The original time complexity was O(n * m) because we searched
18+
* the second array for every item in the first array.
19+
*
20+
* The new time complexity is O(n + m) on average because
21+
* we create a Set and then check the items in the first array.
22+
*
23+
* The space complexity is O(n + m) because the Sets and the
24+
* resulting array can store items from the input arrays.
25+
*
26+
* O(n + m) is optimal because we need to check the arrays
27+
* to find their common items.
728
*
829
* @param {Array} firstArray - First array to compare
930
* @param {Array} secondArray - Second array to compare
1031
* @returns {Array} Array containing unique common items
1132
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
33+
export const findCommonItems = (firstArray, secondArray) => {
34+
const secondSet = new Set(secondArray);
35+
36+
const commonItems = firstArray.filter((item) => secondSet.has(item));
37+
38+
return [...new Set(commonItems)];
39+
};
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
/**
22
* Find if there is a pair of numbers that sum to a given target value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* The original code used two loops to check every possible pair
5+
* of numbers. This can take a long time when the array is large.
6+
*
7+
* I changed the code to use a Set to remember the numbers
8+
* we have already checked.
9+
*
10+
* For each number, we calculate the number we need to find
11+
* to reach the target. If we have already seen that number,
12+
* we return true.
13+
*
14+
* Time Complexity: O(n)
15+
* Space Complexity: O(n)
16+
* Optimal Time Complexity: O(n)
17+
*
18+
* The original time complexity was O(n^2) because we used
19+
* two loops to check the pairs of numbers.
20+
*
21+
* The new time complexity is O(n) on average because we
22+
* only need to go through the array once.
23+
*
24+
* The space complexity is O(n) because the Set can store
25+
* up to all the numbers in the array.
26+
*
27+
* O(n) is the optimal time complexity for this approach
28+
* because we may need to check every number in the array.
729
*
830
* @param {Array<number>} numbers - Array of numbers to search through
931
* @param {number} target - Target sum to find
1032
* @returns {boolean} True if pair exists, false otherwise
1133
*/
1234
export function hasPairWithSum(numbers, target) {
13-
for (let i = 0; i < numbers.length; i++) {
14-
for (let j = i + 1; j < numbers.length; j++) {
15-
if (numbers[i] + numbers[j] === target) {
16-
return true;
17-
}
35+
const seenNumbers = new Set();
36+
37+
for (const num of numbers) {
38+
const neededNumber = target - num;
39+
40+
// Check if we have already seen the number we need
41+
if (seenNumbers.has(neededNumber)) {
42+
return true;
1843
}
44+
45+
// Remember this number for the next checks
46+
seenNumbers.add(num);
1947
}
48+
2049
return false;
2150
}
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
/**
2-
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
2+
* Remove duplicate values from a sequence, preserving the order
3+
* of the first occurrence of each value.
34
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
5+
* The original code used two loops to check whether each value
6+
* was already in the result array.
7+
*
8+
* I changed the code to use a Set. A Set stores each value only
9+
* once and keeps the order in which values were added.
10+
*
11+
* Time Complexity: O(n)
12+
* Space Complexity: O(n)
13+
* Optimal Time Complexity: O(n)
14+
*
15+
* The original time complexity was O(n^2) because we searched
16+
* the result array for every item in the input array.
17+
*
18+
* The new time complexity is O(n) on average because we go
19+
* through the input array once.
20+
*
21+
* The space complexity is O(n) because we may need to store
22+
* all the unique items in the Set and the returned array.
23+
*
24+
* O(n) is optimal because we need to check every item in
25+
* the input array to find all unique values.
726
*
827
* @param {Array} inputSequence - Sequence to remove duplicates from
928
* @returns {Array} New sequence with duplicates removed
1029
*/
1130
export function removeDuplicates(inputSequence) {
12-
const uniqueItems = [];
31+
// Create a Set to store unique items
32+
const uniqueItems = new Set();
1333

14-
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
18-
) {
19-
let isDuplicate = false;
20-
for (
21-
let compareIndex = 0;
22-
compareIndex < uniqueItems.length;
23-
compareIndex++
24-
) {
25-
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
26-
isDuplicate = true;
27-
break;
28-
}
29-
}
30-
if (!isDuplicate) {
31-
uniqueItems.push(inputSequence[currentIndex]);
32-
}
34+
// Go through the input array and add each item to the Set
35+
for (const item of inputSequence) {
36+
uniqueItems.add(item);
3337
}
3438

35-
return uniqueItems;
39+
// Convert the Set back into an array
40+
return [...uniqueItems];
3641
}

0 commit comments

Comments
 (0)