Skip to content

London | Ameneh Keshavarz | Module-Complexity | Sprint 1 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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,14 +9,13 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n)
* Space Complexity: O(1) because we are only using two variables(sum,product) and the space does not grow with input
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
*/
export function calculateSumAndProduct(numbers) {
/*export function calculateSumAndProduct(numbers) {
let sum = 0;
for (const num of numbers) {
sum += num;
Expand All @@ -27,8 +26,26 @@ export function calculateSumAndProduct(numbers) {
product *= num;
}

return {
sum: sum,
product: product,
};
}*/

//Optimal Time Complexity: O(n)
export function calculateSumAndProduct(numbers) {
let sum = 0;
let product = 1;

for (const num of numbers) { //O(n)
sum += num;
product *= num;
}

return {
sum: sum,
product: product,
};
}

// this solution iterates once instead of two
30 changes: 25 additions & 5 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity:O(n * m) because for each element in firstArray, the includes method does a linear search in the second array
* which can take up to m steps, so that would be O(n*m)
*
* Space Complexity: O(n) beacause firstArray.filter() creates an array of at most n items and Set stores at most n items,
* so the overall complexity would be O(n)+O(n) which we consider O(n) as they grow linerly not exponentially
*
*
* @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) => [
/*export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];
];*/

//Optimal Time Complexity: O(n+m+k)

export const findCommonItems = (firstArray, secondArray) => {
const setSecond = new Set(secondArray); // O(m)
const seen = new Set();

for (const item of firstArray) { // )(n)
if (setSecond.has(item)) {
seen.add(item);
}
}

return [...seen]; //)(k) this creates an array from seen which contains only the common items
};

// this solution is more optimised as it reduces Time Complexity. The time complexity of the inital function was quadratic O(n*m) but now it's linear O(n+m+k)
24 changes: 20 additions & 4 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
/**
* 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²) there are two nested for loops
* Space Complexity: O(1) becuae we are not using any extra space
*
* @param {Array<number>} numbers - Array of numbers to search through
* @param {number} target - Target sum to find
* @returns {boolean} True if pair exists, false otherwise
*/
export function hasPairWithSum(numbers, target) {
/*export function hasPairWithSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) {
return true;
}
}
}
return false;
}*/

//Optimal Time Complexity: O(n)

export function hasPairWithSum(numbers, target) {
const seen = new Set();

for (const num of numbers) { // O(n)
const complement = target - num;
if (seen.has(complement)) {
return true;
}
seen.add(num);
}

return false;
}
// the time complexity of original solution was quadratic O(n²) but it's linear for the second solution O(n)
25 changes: 21 additions & 4 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* 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²) because we have two nested loops
* Space Complexity: O(n) uniqueItems array stores up to n elements if all are unique
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
/*export function removeDuplicates(inputSequence) {
const uniqueItems = [];

for (
Expand All @@ -32,5 +31,23 @@ export function removeDuplicates(inputSequence) {
}
}

return uniqueItems;
}*/

//Optimal Time Complexity: O(n) we iterate through the inputSequence once

export function removeDuplicates(inputSequence) {
const seen = new Set();
const uniqueItems = [];

for (const item of inputSequence) {
if (!seen.has(item)) {
seen.add(item);
uniqueItems.push(item);
}
}

return uniqueItems;
}
// the time complexity of original solution was quadratic O(n²) but the it's linear for the second solution