Skip to content

Commit 9c36cab

Browse files
committed
done task for the 1 sprint
1 parent 134e683 commit 9c36cab

4 files changed

Lines changed: 66 additions & 30 deletions

File tree

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity: O(n) because this code has 2 loops that are separated from each other
13+
* Space Complexity: O(n) for input array and O(1) for variables and loops, which result in O(n) in total
14+
* Optimal Time Complexity: O(n) I think this algorithm is efficient enough
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: O(n*m) where n and m are length of each array
5+
* Space Complexity: O(n) where n is a length of arrays
6+
* Optimal Time Complexity: O(n)
77
*
88
* @param {Array} firstArray - First array to compare
99
* @param {Array} secondArray - Second array to compare
1010
* @returns {Array} Array containing unique common items
1111
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
12+
export const findCommonItems = (firstArray, secondArray) => {
13+
// [...new Set(firstArray.filter((item) => secondArray.includes(item))),]
14+
// program flow:
15+
// firstArray.filter go through the each item in first array (loop)
16+
// secondArray.includes check if that item is in the second array (nested loop)
17+
// nested loop result in O(n^2) time complexity
18+
19+
// to reduce time complexity to sublinear on the number of elements in the arrays we can use Set and it's .intersection() method
20+
21+
// complexity of this operation is O(n)
22+
const firstArraySet = new Set(firstArray);
23+
// complexity of this operation is O(m)
24+
const secondArraySet = new Set(secondArray);
25+
// 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
26+
// has O(1) and intersection O(n) which result in O(n) overall time complexity
27+
return Array.from(firstArraySet.intersection(secondArraySet));
28+
};

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,30 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
77
"""
88
Find if there is a pair of numbers that sum to a target value.
99
10-
Time Complexity:
11-
Space Complexity:
12-
Optimal time complexity:
10+
Time Complexity: O(n^2)
11+
Space Complexity: O(n). It's memory for the input array arr
12+
Optimal time complexity: O(n)
1313
"""
14-
for i in range(len(numbers)):
15-
for j in range(i + 1, len(numbers)):
16-
if numbers[i] + numbers[j] == target_sum:
17-
return True
18-
return False
14+
# nested loop result in O(n^2) complexity
15+
# for i in range(len(numbers)):
16+
# for j in range(i + 1, len(numbers)):
17+
# if numbers[i] + numbers[j] == target_sum:
18+
# return True
19+
# return False
20+
21+
# it's better to use to pointers method here
22+
# first: sort the list O(n)
23+
numbers = sorted(numbers)
24+
# declare the left and right pointers
25+
left, right = 0, len(numbers)-1
26+
# and until we "squeezed" the list check if sum of the pointers is equal to target sum
27+
# in worst case we need to do n-1 steps where n is length of the list, so overall complexity will be O(n)
28+
while left < right:
29+
currentSum = numbers[left]+numbers[right]
30+
if currentSum == target_sum:
31+
return True
32+
if currentSum > target_sum:
33+
right -= 1
34+
else:
35+
left+=1
36+
return False

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
77
"""
88
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
10+
Time complexity: O(n^2)
11+
Space complexity: O(n)
12+
Optimal time complexity: 0(n)
1313
"""
14-
unique_items = []
14+
# unique_items = []
1515

16-
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
23-
unique_items.append(value)
16+
# for value in values:
17+
# is_duplicate = False
18+
# for existing in unique_items:
19+
# if value == existing:
20+
# is_duplicate = True
21+
# break
22+
# if not is_duplicate:
23+
# unique_items.append(value)
2424

25-
return unique_items
25+
# return unique_items
26+
# nested array O(n^2) complexity
27+
28+
# 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)
29+
return list(dict.fromkeys(values))

0 commit comments

Comments
 (0)