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) because this code has 2 loops that are separated from each other
* Space Complexity: O(n) for input array and O(1) for variables and loops, which result in O(n) in total
* Optimal Time Complexity: O(n) I think this algorithm is efficient enough
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
Expand Down
26 changes: 20 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(n*m) where n and m are length of each array
* Space Complexity: O(n) where n is a length of arrays
* Optimal Time Complexity: O(n)
*
* @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) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];
export const findCommonItems = (firstArray, secondArray) => {
// [...new Set(firstArray.filter((item) => secondArray.includes(item))),]
// program flow:
// firstArray.filter go through the each item in first array (loop)
// secondArray.includes check if that item is in the second array (nested loop)
// nested loop result in O(n^2) time complexity

// to reduce time complexity to sublinear on the number of elements in the arrays we can use Set and it's .intersection() method

// complexity of this operation is O(n)
const firstArraySet = new Set(firstArray);
// complexity of this operation is O(m)
const secondArraySet = new Set(secondArray);
// intersection method uses has method which is considered to be faster than O(n) and since JS uses hash tables for has methods, the complexity should be
// has O(1) and intersection O(n) which result in O(n) overall time complexity
return Array.from(firstArraySet.intersection(secondArraySet));
};
34 changes: 26 additions & 8 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,12 +7,30 @@ 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(n). It's memory for the input array arr
Optimal time complexity: O(n)
"""
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == target_sum:
return True
return False
# nested loop result in O(n^2) complexity
# for i in range(len(numbers)):
# for j in range(i + 1, len(numbers)):
# if numbers[i] + numbers[j] == target_sum:
# return True
# return False

# it's better to use to pointers method here
# first: sort the list O(n)
numbers = sorted(numbers)
# declare the left and right pointers
left, right = 0, len(numbers)-1
# and until we "squeezed" the list check if sum of the pointers is equal to target sum
# in worst case we need to do n-1 steps where n is length of the list, so overall complexity will be O(n)
while left < right:
currentSum = numbers[left]+numbers[right]
if currentSum == target_sum:
return True
if currentSum > target_sum:
right -= 1
else:
left+=1
return False
30 changes: 17 additions & 13 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ 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: 0(n)
"""
unique_items = []
# unique_items = []

for value in values:
is_duplicate = False
for existing in unique_items:
if value == existing:
is_duplicate = True
break
if not is_duplicate:
unique_items.append(value)
# for value in values:
# is_duplicate = False
# for existing in unique_items:
# if value == existing:
# is_duplicate = True
# break
# if not is_duplicate:
# unique_items.append(value)

return unique_items
# return unique_items
# nested array O(n^2) complexity

# here we iterate through the array to make a dict which is O(n) and than convert it to the list which is also 0(n)
return list(dict.fromkeys(values))