Looking for the most efficient solution for this problem:
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;
};