File tree Expand file tree Collapse file tree 4 files changed +40
-74
lines changed
Expand file tree Collapse file tree 4 files changed +40
-74
lines changed Original file line number Diff line number Diff line change 99 * "product": 30 // 2 * 3 * 5
1010 * }
1111 *
12- * Time Complexity:O(2n) originall have this as using 2 separate loops
13- * Space Complexity:O(1) no extra space used that rows with input
14- * Optimal Time Complexity:O(n) must at least visit each number once
12+ * Time Complexity:
13+ * Space Complexity:
14+ * Optimal Time Complexity:
1515 *
1616 * @param {Array<number> } numbers - Numbers to process
1717 * @returns {Object } Object containing running total and product
1818 */
19-
20- // here we are using 2 loops one for sum and other for product
21- // but we can do it only in one loop so code will be more simple and faster
22-
2319export function calculateSumAndProduct ( numbers ) {
2420 let sum = 0 ;
25- let product = 1 ;
2621 for ( const num of numbers ) {
2722 sum += num ;
23+ }
24+
25+ let product = 1 ;
26+ for ( const num of numbers ) {
2827 product *= num ;
2928 }
29+
3030 return {
3131 sum : sum ,
3232 product : product ,
Original file line number Diff line number Diff line change 11/**
22 * Finds common items between two arrays.
33 *
4- * Time Complexity:O(n+m) it build set and loop through arrays once
5- * Space Complexity: store second array in set
6- * Optimal Time Complexity:O(m+n)
4+ * Time Complexity:
5+ * Space Complexity:
6+ * Optimal Time Complexity:
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- // ];
15- // Refactored to use a Set for faster lookups, making the code more efficient
16- export const findCommonItems = ( firstArray , secondArray ) => {
17- const secondArraySet = new Set ( secondArray ) ;
18- const resultSet = new Set ( ) ;
19- for ( const element of firstArray ) {
20- if ( secondArraySet . has ( element ) ) {
21- resultSet . add ( element ) ;
22- }
23- }
24- return [ ...resultSet ] ;
25- } ;
12+ export const findCommonItems = ( firstArray , secondArray ) => [
13+ ...new Set ( firstArray . filter ( ( item ) => secondArray . includes ( item ) ) ) ,
14+ ] ;
Original file line number Diff line number Diff line change 11/**
22 * Find if there is a pair of numbers that sum to a given target value.
33 *
4- * Time Complexity: o(n2) the function use 2 nested loop
5- * Space Complexity:o(1)no additional significant memory used
6- * Optimal Time Complexity: O(n) — in the refactored version using a Set for lookups
4+ * Time Complexity:
5+ * Space Complexity:
6+ * Optimal Time Complexity:
77 *
88 * @param {Array<number> } numbers - Array of numbers to search through
99 * @param {number } target - Target sum to find
1010 * @returns {boolean } True if pair exists, false otherwise
1111 */
12- // export function hasPairWithSum(numbers, target) {
13- // for (let i = 0; i < numbers.length; i++) {
14- // for (let j = i + 1; j < numbers.length; j++) {
15- // if (numbers[i] + numbers[j] === target) {
16- // return true;
17- // }
18- // }
19- // }
20- // return false;
21- // }
22-
2312export function hasPairWithSum ( numbers , target ) {
24- const numbersNeeded = new Set ( ) ; // stores numbers we've already seen
25-
26- for ( const num of numbers ) {
27- const requiredNumber = target - num ;
28- if ( numbersNeeded . has ( requiredNumber ) ) {
29- return true ; // found a pair!
13+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
14+ for ( let j = i + 1 ; j < numbers . length ; j ++ ) {
15+ if ( numbers [ i ] + numbers [ j ] === target ) {
16+ return true ;
17+ }
3018 }
31- numbersNeeded . add ( num ) ; // remember current number
3219 }
33-
34- return false ; // no pair found
20+ return false ;
3521}
Original file line number Diff line number Diff line change 33ItemType = TypeVar ("ItemType" )
44
55
6- # def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
7- # """
8- # Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
9-
10- # Time complexity:O(n²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times.
11- # Space complexity:O(n) store n elements in worst possible case
12- # Optimal time complexity: O(n) can be improved useing set
13- # """
14- # unique_items = []
15-
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)
6+ def remove_duplicates (values : Sequence [ItemType ]) -> List [ItemType ]:
7+ """
8+ Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
249
25- # return unique_items
10+ Time complexity:
11+ Space complexity:
12+ Optimal time complexity:
13+ """
14+ unique_items = []
2615
27- def remove_duplicates (values : Sequence [ItemType ]) -> List [ItemType ]:
28- unique_element = set () # for unique element
29- order_element = [] # to order maintain
3016 for value in values :
31- if value not in unique_element :
32- unique_element .add (value )
33- order_element .append (value )
34- return order_element
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 )
24+
25+ return unique_items
You can’t perform that action at this time.
0 commit comments