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), as the function iterates the array twice (2N)
* Space Complexity: O(1), the space for variables remains constant
* Optimal Time Complexity: O(N), the function can't be refactored to reduce complexity, (N) is regarded as the same as (2N)
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
Expand Down
20 changes: 14 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(N*M), for each item of loop in the firstArray, it has to loop through the secondArray in the worst case
* Space Complexity: O(N), the worst case is the length of the firstArray
* Optimal Time Complexity: O(N + M), N is for the Set of firstArray, and M is the loop for secondArray. O(N+M) is better than O(N*M).
*
* @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))),
// ];

export const findCommonItems = (firstArray, secondArray) => {
const firstSet = new Set(firstArray);

const commonInSecond = secondArray.filter((item) => firstSet.has(item));

return [... new Set(commonInSecond)];
}
31 changes: 22 additions & 9 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
/**
* 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^2), the loop is inside a loop
* Space Complexity: O(1). O(i) is for i and j index counters.
* Optimal Time Complexity: O(N), it loops once after optimization. However, the space complexity becomes O(N) as there is a growing targetSet.
*
* @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) {
// 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;
// }

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;
}
const targetSet = new Set();
for (let num of numbers) {
const targetNum = target - num;
if (targetSet.has(targetNum)) {
return true;
} else {
targetSet.add(num)
}
}
return false;
}
}
58 changes: 31 additions & 27 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
/**
* 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). It takes a loop inside a loop to compare duplicates.
* Space Complexity: O(N). The uniqueItems array would grow to N in the worst case.
* Optimal Time Complexity: O(N). It only loops once after optimization.
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
// export function removeDuplicates(inputSequence) {
// const uniqueItems = [];

// for (
// let currentIndex = 0;
// currentIndex < inputSequence.length;
// currentIndex++
// ) {
// let isDuplicate = false;
// for (
// let compareIndex = 0;
// compareIndex < uniqueItems.length;
// compareIndex++
// ) {
// if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
// isDuplicate = true;
// break;
// }
// }
// if (!isDuplicate) {
// uniqueItems.push(inputSequence[currentIndex]);
// }
// }

for (
let currentIndex = 0;
currentIndex < inputSequence.length;
currentIndex++
) {
let isDuplicate = false;
for (
let compareIndex = 0;
compareIndex < uniqueItems.length;
compareIndex++
) {
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
uniqueItems.push(inputSequence[currentIndex]);
}
}
// return uniqueItems;
// }

return uniqueItems;
}
export function removeDuplicates(inputSequence) {
return [...new Set(inputSequence)];
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
"sum": 10, // 2 + 3 + 5
"product": 30 // 2 * 3 * 5
}
Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(N), the function loops the array for twice (2N)
Space Complexity: O(1), the space for variables remains constant
Optimal time complexity: O(N), (N) is regarded as the same as (2N)
"""
# Edge case: empty list
if not input_numbers:
Expand Down
21 changes: 12 additions & 9 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(N*M), it is a loop inside a loop
Space Complexity: O(N), the size of the common_items would grow to N in the worst case
Optimal time complexity: O(N + M), N is for the firstSet, M is the filter in second
"""
common_items: List[ItemType] = []
for i in first_sequence:
for j in second_sequence:
if i == j and i not in common_items:
common_items.append(i)
return common_items
# common_items: List[ItemType] = []
# for i in first_sequence:
# for j in second_sequence:
# if i == j and i not in common_items:
# common_items.append(i)
# return common_items

return list(set(first_sequence).intersection(second_sequence))

24 changes: 17 additions & 7 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,22 @@ 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), a loop inside a loop
Space Complexity: O(1). O(1) is for the i and j loop counter.
Optimal time complexity: O(N) as it only loops once, with a trade off space becomes 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
# 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

pair_num_set = set()

for num in numbers:
pair_num = target_sum - num
if pair_num in pair_num_set:
return True
else:
pair_num_set.add(num)
return False
28 changes: 15 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,21 @@ 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). It takes a loop inside a loop
Space complexity: O(N). The space of unique_items would grow to N in the worst case.
Optimal time complexity: O(N). It only loops once after optimization, but the space becomes O(N + 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

return list(dict.fromkeys(values))