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

时间:2019-07-25 14:55:22

标签: javascript arrays algorithm pattern-matching series

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

我试图从数组中获取连续的序列,但这不是我想要的解决方案。

[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

2 个答案:

答案 0 :(得分:0)

请告诉我是否有帮助。.我确信可以对其进行优化。

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

答案 1 :(得分:0)

这是一个依赖函数生成器的解决方案,下面的方法生成所有需要的X元素序列(在您的情况下为2到3)。 该方法以数组作为参数,使用最小大小和最大大小作为参数,并得出每种大小遇到的每个序列(因此,它以[3766,3987]开头,然后以[3987,4046]开头,依此类推)。

接下来,我实现了另一个小方法,该方法从所需的大小开始,返回整数数组中总和最高的元素。当然可以用更好的方法来完成此操作,但是我可以通过单次迭代原始数组来简化事情。

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