Home > Blockchain >  Most efficient solution: An Industrial Company Has N Factories, Each Producing Some Pollution Every
Most efficient solution: An Industrial Company Has N Factories, Each Producing Some Pollution Every

Time:03-20

Looking for the most efficient solution for this problem:

Given question

My solution:

public int solution(int[] A)
{
    float targetPolution = A.Sum() / 2;
    float filteredPolution = 0;
    int totalFilters = 0;
    A = A.OrderByDescending(c => c).ToArray();
    for (int i = 0; i < A.Length; i  )
    {
        totalFilters  ;
        filteredPolution  = (float)A[i] / 2;
        if (filteredPolution >= targetPolution)
            break;
    }
    return totalFilters;
}

CodePudding user response:

float targetPolution = A.Sum() / 2f;
var filteredPolution = A.Select(x => (float)x).ToArray();
int totalFilters = 0;
while (filteredPolution.Sum() > targetPolution)
{
    totalFilters  ;
    int imax = Array.IndexOf(filteredPolution, filteredPolution.Max());
    filteredPolution[imax] /= 2;
};
  •  Tags:  
  • c#
  • Related