Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Example

Example

##Example

Example

added 60 characters in body
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

I would say that if (hashMapB.hasOwnProperty(difference)) { is redundant as it can be assumed that objectobjects within the environment of leetcode will not have enumerable properties higher in the prototype chain to worry about. Thus if (hashMapB[difference] !== undefined) or wouldwould be more performant.

JS will automatically insert semicolons, but there are many situations where towhen the location of the insertioninserted semicolon is not obvious. Inexperienced JS coders should always use semicolons. Experienced coders know its easier to include them.

The link you provided is not a suitabletestable problem, rather it is just a leetcode discussion topic. I have nothing to test on apart from the limited arguments provided in the discussion. As thereThere are many question that the set of given inputs doesdo not answer, writing the optimal solution is not possible. Rather theThe example below is just a brute force solution of complexity \$O(nm)\$ where \$n\$ and \$m\$ are the length of the two input arrays.

Comparing this function against yours and assuming that the values to sum are signed integers (not unsigned as that would allow the inner loop to be skipped if an items had a value greater than the sum (3rd arg)). It

The example returns a result in 0.589μs against your functions 25.250μs (μs = 1/1,000,000 second)

To avoid creating a new array each time a larger max sum is found I use a counter foundCount as an index of where to put new closer indexes in the result array. When the function is done I just trim the array by setting its length to the found count.

I would say that if (hashMapB.hasOwnProperty(difference)) { is redundant as it can be assumed that object within the environment of leetcode will not have enumerable properties higher in the prototype chain to worry about. Thus if (hashMapB[difference] !== undefined) or would be more performant.

JS will automatically insert semicolons, but there are many situations where to location of the insertion is not obvious. Inexperienced JS coders should always use semicolons. Experienced coders know its easier to include them.

The link you provided is not a suitable problem, rather just a discussion topic. I have nothing to test on apart from the limited arguments provided in the discussion. As there are many question that the set of inputs does not answer writing the optimal solution is not possible. Rather the example below is just a brute force solution of complexity \$O(nm)\$ where \$n\$ and \$m\$ are the length of the two input arrays.

Comparing this function against yours and assuming that the values to sum are signed integers (not unsigned as that would allow the inner loop to be skipped if an items had a value greater than the sum (3rd arg)). It returns a result in 0.589μs against your functions 25.250μs (μs = 1/1,000,000 second)

To avoid creating a new array each time a larger max sum is found I use a counter foundCount as an index to put new closer indexes. When the function is done I just trim the array by setting its length to the found count.

I would say that if (hashMapB.hasOwnProperty(difference)) { is redundant as it can be assumed that objects within the environment of leetcode will not have enumerable properties higher in the prototype chain to worry about. Thus if (hashMapB[difference] !== undefined) would be more performant.

JS will automatically insert semicolons, but there are many situations when the location of the inserted semicolon is not obvious. Inexperienced JS coders should always use semicolons. Experienced coders know its easier to include them.

The link you provided is not a testable problem, rather it is just a leetcode discussion topic. I have nothing to test on apart from the limited arguments provided in the discussion. There are many question that the set of given inputs do not answer, writing the optimal solution is not possible. The example below is just a brute force solution of complexity \$O(nm)\$ where \$n\$ and \$m\$ are the length of the two input arrays.

Comparing this function against yours and assuming that the values to sum are signed integers (not unsigned as that would allow the inner loop to be skipped if an items had a value greater than the sum (3rd arg)).

The example returns a result in 0.589μs against your functions 25.250μs (μs = 1/1,000,000 second)

To avoid creating a new array each time a larger max sum is found I use a counter foundCount as an index of where to put new closer indexes in the result array. When the function is done I just trim the array by setting its length to the found count.

Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

Your code seams massively over complex.

I do not see the need to sort the arrays as the overhead does not warrant the the benefit.

Assigning to the maps in the sort function is very inefficient. The sort function will be called many more times than there are items in the array.

I would say that if (hashMapB.hasOwnProperty(difference)) { is redundant as it can be assumed that object within the environment of leetcode will not have enumerable properties higher in the prototype chain to worry about. Thus if (hashMapB[difference] !== undefined) or would be more performant.

The long variable names makes your code hard to follow. Consider short names and use common abbreviations to reduce the length of lines.

JS will automatically insert semicolons, but there are many situations where to location of the insertion is not obvious. Inexperienced JS coders should always use semicolons. Experienced coders know its easier to include them.

You don't need to delimit single line statement blocks with {} however it is a source of future bugs when modifying code, as adding the {} can easily be overlooked and very hard to spot. Always delimit all statement blocks. eg if (foo) bar =1 is better as if (foo) { bar = 1 }

The link you provided is not a suitable problem, rather just a discussion topic. I have nothing to test on apart from the limited arguments provided in the discussion. As there are many question that the set of inputs does not answer writing the optimal solution is not possible. Rather the example below is just a brute force solution of complexity \$O(nm)\$ where \$n\$ and \$m\$ are the length of the two input arrays.

##Example

Comparing this function against yours and assuming that the values to sum are signed integers (not unsigned as that would allow the inner loop to be skipped if an items had a value greater than the sum (3rd arg)). It returns a result in 0.589μs against your functions 25.250μs (μs = 1/1,000,000 second)

I did not test it against very large data sets nor did I look too deeply for a less complex solution.

To avoid creating a new array each time a larger max sum is found I use a counter foundCount as an index to put new closer indexes. When the function is done I just trim the array by setting its length to the found count.

function currentHighest(a, b, max) {
 const lenA = a.length, lenB = b.length, result = [];
 var i = 0, j, foundCount = 0, maxFound = -Infinity;
 while (i < lenA) {
 j = 0;
 const pA = a[i], valA = pA[1];
 while (j < lenB) {
 const pB = b[j], sum = valA + pB[1];
 if (sum <= max && sum >= maxFound) {
 if (sum !== maxFound) { foundCount = 0 } 
 maxFound = sum;
 result[foundCount++] = [pA[0], pB[0]];
 }
 j++;
 }
 i++;
 }
 result.length = foundCount;
 return result;
}
default

AltStyle によって変換されたページ (->オリジナル) /