#codingexercise
Problem: Given two container measurements of a and b used to distribute a commodity with a total quantity of d to n customers , determine the maximum number of customers who can be satisfied with their individual requirements of a and b
Solution: use a greedy strategy to take the increasing order aggregated demands of a and b together for each person which helps us maximize the customer count
List<int> GetSatisfiedCustomers(List<Tuple<int,int>> demands, int d)
{
assert(demands != null);
assert(demands.Count() > 0);
assert (d > 0);
Converter<Tuple<int,int>, Tuple<int,int>> converter =
(item,index) => {
return new Tuple<int, int> ( item.first*a+item.second*b, index );
};
var aggregated = demands.ConvertAll<Tuple<int,int>>(converter).ToList();
aggregated.sort(); // by aggregated demand
var customers = new List<int>();
aggregated.ForEach( demandByPerson => {
if (demandByPerson.First > d) {
d -= demandByPerson.First;
customers.Add(demandByPerson.Second);
}
});
return customers;
}
The above method merely tries to represent the individual demand of the users in unit measurements and then sort them in the increasing order. With the help of the increasing numbers for the demands, we are now able to include as many customers as we can.