如何获取包含最小差异数组元素的数组

时间:2017-10-26 13:09:14

标签: c# arrays algorithm equation-solving

[Image of the actual problem]

我们必须根据input3选择最佳项目/项目,这是要选择的项目。选择应该是我们不总是采取最大项目。相反,我们采取没有太大差异的项目。

input1: total items
input2: array of items
input3: items to be selected

Scenario 1:
input: 6, {44,55,605,100,154,190}, 1
output should be: {605}

input: 5, {15,85,32,31,2}, 2
output should be: {32,31}

当我们增加要选择的项目数时,输出应该选择更多的项目,并且差异最小。 以下是我正在尝试的代码,我是新手,请帮助: 我被困在如何使这种动态。

public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
{
    var i = values;
    int[] results = new int[totalToBeSelected];

    var resultList = new List<int>();

    if (totalToBeSelected == 1)
    {
        resultList.Add(values.Max());
        return resultList.ToArray();
    }
    Array.Sort(i);
    var minmumDiff = (i[0] - i[1]) * -1;

    for (int k = 1; k < i.Length; k++)
    {
        var differnce = i[k] - i[k - 1];

        if (differnce < minmumDiff)
        {
            resultList.Add(i[k]);
            resultList.Add(i[k - 1]);
            minmumDiff = differnce;
        }
    }
    return resultList.ToArray();
}

2 个答案:

答案 0 :(得分:1)

问题的条件不明确,必须作出一些假设。

class Program
{
        static void Main(string[] args)
        {
            var items = new[] {12,14,22,24,6};//new[] { 15, 85, 32, 31, 2};//new[] { 44, 55, 605, 100, 154, 190 };
            var totalItems = items.Count();
            var numberOfItemsToSelect = 3;

            var result = Find(totalItems, items, numberOfItemsToSelect);            

            PrintList(result);

            Console.ReadLine();
        }

        static void PrintList(IEnumerable<int> scoreList)
        {
            foreach (var score in scoreList)
            {
                Console.Write(score);
                Console.Write(" ");
            }
        }

        public static int[] Find(int totalItems, int[]values, int totalTobeSelected)
        {
            var result = new List<int>();
            if (totalTobeSelected <= 1)
            {
                result.Add(values.Max());

            }
            else if (totalTobeSelected == totalItems)
            {
                result.AddRange(values.OrderBy(i => i).ToList());
            }
            else
            {

                var mainSet = values.OrderBy(i => i).ToList();
                var setDic = new Dictionary<int, IEnumerable<int>>();

                for (int i = 0; (totalItems - i >= totalTobeSelected); i++)
                {
                    var set = mainSet.GetRange(i, totalTobeSelected);

                    //Inside a set, we choose the difference between the first and the second number
                    // ex: set = {2, 4, 9} => diff = |2-4| = 2.
                    var diff = Math.Abs(set[0] - set[1]);

                    // given two sets with the same diff, we select the first one base on the sort order of the main set:
                    // ex: main set = {2,4,8,10}. Both {2,4} and {6,8} have a diff of 2 so we select {2,4}
                    if (setDic.ContainsKey(diff)) continue;
                    setDic.Add(diff, set);

                }

                if (setDic.Count > 0)
                {
                    var minKey = setDic.Keys.Min();
                    result.AddRange(setDic[minKey]);
                }

            }
            return result.ToArray();
        }
    }

答案 1 :(得分:1)

您可以查看此功能。

    public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
    {
        Array.Sort(values);
        Array.Reverse(values); // We need any value greater than max items diff. Max array item (first item after the sort) enough for it.
        int diff = values[0]; 
        int indx = 0;
        for (int i = 0; i < totalItems - totalToBeSelected +1; i++)
        {
            int temp_diff = values[i] - values[i + totalToBeSelected - 1]; // We are looking for any items group that max and min value difference is minimum 
            if (temp_diff < diff )
            {
                diff = temp_diff;
                indx = i;
            }
        }

        int[] results = new int[totalToBeSelected];
        Array.Copy(values, indx, results, 0, totalToBeSelected);

        return results;
    }

样品:

        Find( 6, new int[] { 44, 55, 605, 100, 154, 190 }, 1 );
        Out: { 605 }

        Find( 5, new int[] { 15, 85, 32, 31, 2 }, 2 );
        Out: { 32, 31 }