【问题标题】:Most efficient way to find every max number in a set of a numbers在一组数字中找到每个最大数字的最有效方法
【发布时间】:2017-03-22 20:24:50
【问题描述】:

我有 3 个 int 值:

int value1;
int value2;
int value3;

还有 3 个布尔值:

bool maxIs1;
bool maxIs2;
bool maxIs3;

输入值必须是单独的变量。

maxIs1 = True 表示value1 必须有最大值等等。

我需要将这组数字与一组条件进行比较的方法。 例如:

int value1 = 10;
int value2 = 1;
int value3 = 10;

bool maxIs1 = True;
bool maxIs2 = False;
bool maxIs3 = True;

bool result = compareValues(); //true

或者:

int value1 = 1;
int value2 = 1;
int value3 = 10;

bool maxIs1 = True;
bool maxIs2 = False;
bool maxIs3 = True;

bool result = compareValues(); //false

最有效的方法是什么?

【问题讨论】:

  • 变量的数量是恒定的 (3) 还是可能有其他变量?
  • 就我而言,这里只有 3 个。
  • 如果您担心担心效率和超过 3 个值,这是O(n) 问题,您应该只需要迭代输入一次。

标签: c# max


【解决方案1】:

这很有趣。如果可以将它们放入数组中,则可以使用 linq 干净地检查它们是否都满足条件:

var values = new[] { value1, value2, value3 };
var maxes = new[] { maxIs1, maxIs2, maxIs3 };
var max = values.Max();

var result = values
    .Zip(maxes, (f, s) => new { value = f, isMax = s })
    .All(c => !c.isMax || (c.value == max));

【讨论】:

  • 如果值为最大值但布尔值表示不满足条件,您的答案是否满足条件,我认为在这种情况下它应该返回 false
  • @AliEzzatOdeh:如果这是您想要的行为,您可以将最后一行更改为 .All(c => (!c.isMax && c.value != max) || (c.isMax && c.value == max));
【解决方案2】:

我认为在 3 个值的情况下您不必关心效率

int value1 = 10;
int value2 = 1;
int value3 = 1;

bool maxIs1 = true;
bool maxIs2 = false;
bool maxIs3 = true;

int max = new[] { value1, value2, value3 }.Max();
bool result = (!maxIs1 || value1 == max) && (!maxIs2 || value2 == max) && (!maxIs3 || value3 == max);

【讨论】:

    【解决方案3】:

    如果您想要不同数量的值,请尝试此解决方案:

    static void Main()
        {
            List<int> listOfInts = new List<int>();
            List<bool> listOfBools = new List<bool>();
    
            listOfInts.Add(1);
            listOfInts.Add(2);
            listOfInts.Add(1);
    
            listOfBools.Add(false);
            listOfBools.Add(false);
            listOfBools.Add(false);
    
            Console.WriteLine("value = " + Compare(listOfInts,listOfBools));
    
        }
    

    Compare() 应该是这样的:

    static bool Compare(List<int> listOfInts, List<bool> listOfBools)
        {
            int max = listOfInts.Max();
            bool isCorrect = true;
    
            var maxList = listOfInts.Where(value => value == max);
            var indicesOfMax = GetIndeciesOfMax(maxList, listOfInts);
    
            for (int i = 0; i < listOfInts.Count; i++)
            {
                if (indicesOfMax.Contains(i) && listOfInts[i] == max && listOfBools[i])
                {
                    isCorrect = true;
                }
                else if (!indicesOfMax.Contains(i) && listOfInts[i] != max && !listOfBools[i])
                {
                    isCorrect = true;
                }
                else
                {
                    isCorrect = false;
                    break;
                }
            }
    
            return isCorrect;
        }
    
        static List<int> GetIndeciesOfMax(IEnumerable<int> maxList, List<int> list)
        {
            List<int> indecies = new List<int>();
    
            foreach (var m in maxList)
            {
                indecies.Add(list.IndexOf(m));
            }
    
            return indecies;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多