asp.net - C# - Combinatorics -


i have list of ~300 objects have price , score. need find best combination (i.e. highest total score) of 15 of objects total price less x.

the straightforward way this, see it, nest 15 loops , check every possible combination, take days.

is there 'clean' way in c#?

thanks!

it difficult without example, if understand problem might help.

assuming object looks this

public class item {         public int score { get; set; }         public decimal price { get; set; } } 

then following should sort out.

var listofobjects = new list<item>();  var topitems = listofobjects.where(p => p.price < 100).orderbydescending(p => p.score).take(15); 

edit : after details disclosed, following should help

disclaimer : quick , dirty solution (sub optimal)

create new class

public class itemwithrunningtotal {     public item item { get; set; }     public decimal runningtotal { get; set; } } 

then following should need.

var maxtotal = 1500; //for 8000         var objects = new list<item>()                       {                           new item() {score = 10, price = 100},                           new item() {score = 20, price = 800},                           new item() {score = 40, price = 600},                           new item() {score = 5, price = 300},                       };          decimal runningtotal = 0;         var newlist = objects             .orderbydescending(p => p.score)             .select(p =>                     {                         runningtotal = runningtotal + p.price;                         return new itemwithrunningtotal()                                {                                    item = p,                                    runningtotal = runningtotal                                };                     })             .orderbydescending(p => p.runningtotal)             .where(p => p.runningtotal <= maxtotal).take(15); 

Comments