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,26 @@
* "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)
*
* It loops twice: Once to compute the sum and Once to compute the product.
*
* Time Complexity

Let n be the number of elements in numbers.

Operations:

First loop: visits each element = O(n)
Second loop: visits each element again = O(n)
Even though there are two separate loops, each runs in linear time. Therefor, Time Complexity: O(n)
Space Complexity

We're not allocating any new data structures proportional to n.
Space Complexity: O(1)
(No extra space used that grows with input size.)
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
Expand All @@ -32,3 +49,20 @@ export function calculateSumAndProduct(numbers) {
product: product,
};
}


// optimal solution

//make it even cleaner by combining both operations into a single loop, like this. But O(n) is the best We can do for time complexity.

export function calculateSumAndProduct(numbers) {
let sum = 0;
let product = 1;

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

return { sum, product};
}
39 changes: 37 additions & 2 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Time Complexity: O(n x m)
* Space Complexity: O(n)
* Optimal Time Complexity:
*
* @param {Array} firstArray - First array to compare
Expand All @@ -12,3 +12,38 @@
export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];


/*
Time Complexity:

n = firstArray.length
m = secondArray.length

filter() loops over firstArray = O(n)
Inside filter, includes() searches secondArray = O(m)
Combined: O(n x m)
new Set(...) and spreading it: O(k) (where k is number of common items)
Overall Time Complexity: O(n x m)


Space Complexity:

We’re creating temporary arrays and a set, all of which scale with the size of firstArray.
The overall space complexity is O(n).

*/

// Optimal Solution

export const findCommonItems2 = (firstArray, secondArray) => {
const set2 = new Set(secondArray);
return [...new Set(firstArray.filter(item => set2.has(item)))];

};

// now set2.has(item) is O(1) (instead of .includes() which is O(m))

//.has() is a constant-time lookup

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
34 changes: 34 additions & 0 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 @@ -16,3 +16,37 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
if numbers[i] + numbers[j] == target_sum:
return True
return False


'''
Time Complexity:

Outer loop: i goes from 0 to n-1 = O(n)

Inner loop: j goes from i+1 to n-1 = O(n) (in worst case)

total comparison = n * (n - 1) / 2

Therefor total complexity = O(n²)

Space Complexity:

not creating new lists, sets, or hash maps. Only using loop variables i, j, and a few constants.

Space Complexity: O(1)
'''


#optimal


def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
seen = set()
for num in numbers:
complement = target_sum - num
if complement in seen:
return True
seen.add(num)
return False

## Optimal for Time complexity. Reduces it to O(n), but increases the space complexity to O(n).
30 changes: 30 additions & 0 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,33 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
unique_items.append(value)

return unique_items


"""
Time Complexity:

1 + 2 + 3 + ... + (n - 1) = n(n - 1)/2 = O(n²)

Space Complexity:

Even though we're just using .append(), the list grows linearly, so it is s O(n) space.

"""

#Optimal Solution

def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
seen = set()
unique_items = []

for value in values:
if value not in seen:
seen.add(value)
unique_items.append(value)

return unique_items

'''
Time Complexity: O(n)
Space Complexity: O(n) storing unique_items and seen
'''
Loading