确定整数数组中最长的变化周期

时间:2018-11-16 11:39:03

标签: javascript arrays algorithm

我正在尝试编写一个函数,该函数将识别数字数组中最长的变化周期。当前一个数字大于当前数字时,方差开始;当下一个数字与当前数字相同时,方差结束。但是,如果方差没有结束,则假定方差以最后两个数字开始。

例如:[10, 5, 3, 11, 8, 9, 9, 2, 10]此数组中最长的变化周期为[5, 3, 11, 8, 9],或仅为5(长度)。当以下数字与当前数字相同时,方差结束,在这种情况下为9

我写的函数适用于这种情况;但是,不是整个数组都具有方差时,例如[10, 5, 10, 5, 10, 5, 10, 5, 10]应当返回8时返回9

在前一个数字为number的情况下,总会小于或总是大于2,因为它永远不会结束。例如[2, 4, 6, 8][8, 6, 4, 2]

我知道可以通过在0处启动for循环来解决整个数组方差的问题,但是其他情况将变得无效。任何帮助将不胜感激。

事不宜迟,这是我的代码:

function findVariance(numbers) {
    if ([0,1].includes(numbers.length)) return numbers.length;

    const variance = [[0]];
    let greater = numbers[1] > numbers[0];
    let lesser = numbers[1] < numbers[0];

    for (let i = 1; i < numbers.length; i++) {
        let previous = variance.length - 1;
        let previousVarianceGroup = variance[previous];
        let previousVarianceGroupValue = previousVarianceGroup[previousVarianceGroup.length - 1];

        if (greater) {
            if (numbers[i] <  numbers[previousVarianceGroupValue]) {
                previousVarianceGroup.push(i);
                greater = false;
                lesser = true;
            } else {
                greater = numbers[i] < numbers[previousVarianceGroupValue];
                lesser = numbers[i] < numbers[previousVarianceGroupValue];
                variance.push([previousVarianceGroupValue, i]);
            }
        } else if (lesser) {
            if (numbers[i] > numbers[previousVarianceGroupValue]) {
                previousVarianceGroup.push(i);
                greater = true;
                lesser = false;
            } else {
                greater = numbers[i] > numbers[previousVarianceGroupValue];
                lesser = numbers[i] > numbers[previousVarianceGroupValue];
                variance.push([previousVarianceGroupValue, i]);
            }
        } else {
            greater = numbers[i] > numbers[previousVarianceGroupValue];
            lesser = numbers[i] < numbers[previousVarianceGroupValue];
            variance.push([previousVarianceGroupValue, i]);
        }
    }

    const result = [];

    for (let i = 0; i < variance.length; i++) {
        result[i] = variance[i].length;
    }

    result.sort();

    return result[result.length - 1];
}

console.log(findVariance([10, 5, 3, 11, 8, 9, 9, 2, 10]));
console.log(findVariance([10, 5, 10, 5, 10, 5, 10, 5, 10]));
console.log(findVariance([2, 4, 6, 8]));

2 个答案:

答案 0 :(得分:1)

这就是我得到的(试图尽我所能来理解这个问题)

function calculateVariance(arr) {
    // trivial cases
    if (arr.length <= 1) { return arr.length; }

    // store the difference between each pair of adjacent numbers
    let diffs = [];
    for (let i = 1; i < arr.length; i++) {
        diffs.push(arr[i] - arr[i - 1]);
    }

    let max = 0;

    // if the difference between two numbers is 0, they're the same.
    // the base max variance encountered is 1, otherwise it's 2.
    // the boolean zen here is that diffs[0] is falsy when it's 0, and truthy otherwise
    let count = diffs[0] ? 2 : 1;

    // go through the array of differences,
    // and count how many in a row are alternating above/below zero.
    for (i = 1; i < diffs.length; i++) {
        if ((diffs[i] < 0 !== diffs[i - 1] < 0) && diffs[i] && diffs[i - 1]) {
            count++;
        } else {
            max = Math.max(count, max);
            // see above
            count = diffs[i] ? 2 : 1;
        }
    }

    // account for the maximum variance happening at the end
    return Math.max(count, max);
}

答案 1 :(得分:0)

您使事情有些复杂化,只要增加一个计数器,只要一个元素等于下一个元素,就重置为等于即可:

 const counts = [];

 let count = 0;
 for(let i = 0; i < numbers.length - 1; i++) {
   if(numbers[i] === numbers[i + 1]) {
     counts.push(count);
     count = 0;
   } else {
      count++;
  }
}

 counts.push(count);

return counts.sort()[counts.length - 1];