【问题标题】:How to find the first and second highest series of numbers in array如何在数组中找到第一和第二高的数字系列
【发布时间】:2019-07-25 14:55:22
【问题描述】:

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

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

[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

【问题讨论】:

  • 每个系列的长度是多少?如果没有限制,则系列可以是整个数组。
  • 系列的定义是什么? 3766, 3987, 4046, 4187, 4489 不也是一个系列吗?
  • [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] 的理想解决方案是什么,当你回答时,为什么不给它增加一个价值呢?
  • 还有when sum() and highest by length as well - 你不能同时最大化两个值。您想要两个单独的计算?
  • 对不起,我应该提到。最大系列长度限制为 3。@KeroppiMomo

标签: javascript arrays algorithm pattern-matching series


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这是一个依赖函数生成器的解决方案,下面的方法生成所有需要的 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);

    【讨论】:

    • 哇.. 我喜欢你的代码。有一天我会到达那里:) 但我不确定它是否提供所需的简历.. 或者我错了。
    • 嗯,它给出了他的要求,我已经仔细检查了结果,排序实际上是正确的:P。无论如何,如果预期结果不同,编辑排序就足够了,我不确定他是想要最大的还是所有的对。如果这对是想要的结果,那其实很简单。
    • 就像我说的我可能是错的 :) 嘿.. 说真的!喜欢你的代码!
    • @FordAnderson 谢谢,当需要生成时,我个人更喜欢生成器方法而不是其他方法......我觉得它使代码更容易阅读:)
    • 正是.. 你会推荐一本书或一个网站来学习 JS mate 吗?今天从你的代码中学到了一些东西。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2018-01-10
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    相关资源
    最近更新 更多