如何找到数组中第一和第二高的数字系列

How to find the first and second highest series of numbers in array

我有一个数字数组,我想从数组中找到最高的数字系列。

我试过从一个数组中获取连续的系列,但这不是我正在寻找的解决方案。

[3766, 3987, 4046, 4187, 4489, 3700, 3307, 3336, 4113, 4286, 1278, 5676, 3140, 3299, 2617, 4928, 4498]

这是我的数组,我想在 sum() 时找到最高的系列 也是长度最高的,最大系列长度为 3。 所以实际上我期待这些结果。 4046, 4187, 4489

有不明白的地方请追问。 第一高系列 4046, 4187, 4489 第二高的是 4928, 4498

如果这有帮助,请告诉我。我相信它可以优化。

var arr = [3766, 3987, 4046, 4187, 4489, 3700, 3307, 3336, 4113, 4286, 1278, 5676, 3140, 3299, 2617, 4928, 4498];
var maxLength = 3; //Maximum length of series
var curLength = 0; //Keeping track of current length

var series = []; //Series that makes maximum sum
var tmpSeries = []; 
var currentMax = 0; //Current maximum value.

findSeries();

function findSeries()
{
    var tmpMax = 0;
    for (var i = 0; i < arr.length; i++) {

        var isInc = curIncStat = (arr[i] > arr[i +1]) ? "I" : "D";

        /*Iterate till one of the condition fails.
        1. maxLength is reached
        2. sequence changes from Inc to Dec
        3. Run out of array
        */

        while(true)
        {
            //Checking for fail conditions
            if(curIncStat != isInc || curLength == maxLength || (i + 1) > arr.length)
            {
                //Checking if tmpSeries sum is greater than currentMax
                if(tmpMax >  currentMax)
                {
                    series = [];
                    series.push(tmpSeries);
                    currentMax = tmpMax;

                }

                //Resetting all values.
                tmpMax = 0;
                tmpSeries = [];
                curLength = 0;
                break;
            }

            tmpSeries.push(arr[i + curLength]);
            tmpMax += arr[i + curLength];

            curIncStat = (arr[i + curLength] > arr[i + curLength + 1]) ? "I" : "D";
            curLength++;
        }
    }
    console.log(arr)
    console.log(series);
    console.log(currentMax);
}

这是一个依赖于函数生成器的解决方案,下面的方法生成所有需要的 X 元素序列(在您的例子中是 2 到 3)。 该方法将数组作为参数、最小大小和最大大小,针对每个大小生成它遇到的每个序列(因此,它以 [3766, 3987] 开头,然后是 [3987, 4046] 等等)。

接下来,我实现了另一个小方法,即 returns 整数数组中总和最高的元素,从所需大小开始。这当然可以通过更好的方式完成,但我认为一次性迭代原始数组会使事情更容易管理。

const input = [3766, 3987, 4046, 4187, 4489, 3700, 3307, 3336, 4113, 4286, 1278, 5676, 3140, 3299, 2617, 4928, 4498];

/*
  Builds all the sequences from left to right of an array, of all the sizes between minSize and maxSize included.
*/
function* buildSequences(arr, minSize, maxSize) {
  for (let i = minSize; i <= maxSize; i++) {
    let counter = 0;
    do {
      yield arr.slice(counter, counter + i);
      counter++;
    }
    while (counter <= arr.length - i);
  }
}

// Acquire the highest element in an array of arrays, by returning the element with the highest sum and specified size (length).
const getHighestBySerieSize = (arr, size) => {
  let sumArray = (a) => a.reduce((a,b) => a + b);
  return arr.filter(i => i.length === size).sort((a,b) => sumArray(a) < sumArray(b))[0];
}

// Build all sequences of 2 to 3 elements, fromm left to right.
const sequences = [...buildSequences(input, 2, 3)];

// Find the highest of each.
const [highestThreeSerie, highestTwoSerie] = [getHighestBySerieSize(sequences, 2), getHighestBySerieSize(sequences, 3)];

console.log(highestThreeSerie, highestTwoSerie);