如何减少0(n)以找到对特定值求和的对的第一个实例

时间:2017-04-03 11:25:23

标签: javascript arrays performance time-complexity

我正在做一个代码问题,说明如下:给定一个整数列表和一个和值,返回前两个值(请从左边解析)按照外观的顺序加起来形成总和

解决方案有效,但对于长数组来说太慢了,如果不使用两个for循环,有人会怎么做?我一直在努力减少时间复杂度,但是当我需要查看所有可能的对时,我对如何实现这一点感到茫然。

function sumPairs(ints, s){
    var lowestIdx1 = Infinity;
    var lowestIdx2 = Infinity;

    for (var i = 0; i < ints.length-1; i++) {
        var cur = ints[i]
        for (var k = i+1; k < ints.length; k++) {
            var next = ints[k]
            if(cur + next === s){
                if(i <= lowestIdx1 && k <= lowestIdx1 || i <= lowestIdx2 && k <=lowestIdx2){
                    lowestIdx1 = i
                    lowestIdx2 = k
                }
            }
        }
    }
    if(lowestIdx1 !== Infinity){    
        return [ints[lowestIdx1], ints[lowestIdx2]]
    }
}

为了更清楚这里的问题,有一些示例输入输出:

sum_pairs([11, 3, 7, 5],         10)
#              ^--^      3 + 7 = 10
== [3, 7]

sum_pairs([4, 3, 2, 3, 4],         6)
#          ^-----^         4 + 2 = 6, indices: 0, 2 *
#             ^-----^      3 + 3 = 6, indices: 1, 3
#                ^-----^   2 + 4 = 6, indices: 2, 4
#  * entire pair is earlier, and therefore is the correct answer
== [4, 2]

sum_pairs([0, 0, -2, 3], 2)
#  there are no pairs of values that can be added to produce 2.
== undefined

2 个答案:

答案 0 :(得分:2)

你可以使用一些超速机制,比如

  • 单循环,
  • 访问值的哈希表
  • 元素a
  • 变量array[i]

Sum of Pairs on Codewars需要的长列表 153 ms

var sum_pairs = function (array, s) {
    var a, i,
        hash = Object.create(null);

    for (i = 0; i < array.length; i++) {
        a = array[i];
        if (hash[s - a]) {
            return [s - a, a];
        }
        if (!hash[a]) {
            hash[a] = true;
        }
    }
};

console.log(sum_pairs([11, 3, 7, 5], 10));        // [3, 7]
console.log(sum_pairs([4, 3, 2, 3, 4], 6));       // [4, 2]
console.log(sum_pairs([0, 0, -2, 3], 2));         // undefined
console.log(sum_pairs([10, 5, 2, 3, 7, 5], 10));  // [3, 7]
console.log(sum_pairs([1, 2, 3, 4, 1, 0], 2));    // [1, 1]
console.log(sum_pairs([1, -2, 3, 0, -6, 1], -6)); // [0, -6]
console.log(sum_pairs([0, 2, 0], 0));             // [0, 0]
console.log(sum_pairs([5, 9, 13, -3], 10));       // [13, -3]
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

下面的解决方案在 O(n) 时间内运行,查看它是如何解决的步骤:

// steps
// loop through array
// for each member
// first check if it's value in hash
// then store in hash with key as sum-member
// and value as member
// if value in hash already
// return [k,v]

function sumPairs(ints, s) {

  const possible_pairs={}

  // loop through array
  for(let ints_i=0;ints_i<ints.length;ints_i+=1){

    // for each member
    let element = ints[ints_i].toString()

    // first check if it's value in hash
    // if value in hash already
// return [k,v]
    if (possible_pairs[element]) return [parseInt(possible_pairs[element], 10), parseInt(element, 10)]

      // else store in hash with key as value-member
  // and value as member
    possible_pairs[s-element]=element
  }

  return undefined ;
}
console.log(sumPairs([ 0, -6], -6)) //[0, -6]
console.log(sumPairs([10, 5, 2, 3, 7, 5], 10)) //[3, 7]