javascript - 识别整数数组中最长的方差周期

标签 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 时。

在前一个数字总是小于或总是大于方差的情况下,方差将为 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]));

最佳答案

这是我得到的(尽我所能理解问题)

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);
}

关于javascript - 识别整数数组中最长的方差周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53337130/

相关文章:

javascript - 如何从字符串中删除最后一个 '[]'?

javascript - 在代码运行时停止事件监听器?

javascript - Sencha 从 JSON 记录填充选项卡

javascript - 将对象数组(构建细节)组合成一个对象(将构建名称作为顶级键)

ios - swift 3 : Using Two Arrays to populate grouped Table

algorithm - DP 左上角与右下角表格填充。什么时候使用哪个?

javascript - 如何移除 Canvas

java - 如何从输入中读取一系列数字并将它们存储到数组中?

algorithm - 具有重复的间隔数组的联合中的第 n 个最小元素

algorithm - 从输入和输出字节确定压缩算法