开发者

Fair Sorting two arrays by values in another array

In ruby, I need to sort an array that contains ten hashes, in to two arrays.

Each hash has an entry named "points"

I need to sort the two 开发者_高级运维arrays to be most fair on both parts.

thus if one hash has 1200 points, and another has 1220, and the rest 1000, I need to sort it out so the ones with 1200 and 1220 points doesnt get in the same array, if you follow :)

Hope the good folks of aardvark can help me!


This is called the partition problem. It is NP-complete, but according to Wikipedia "there are heuristics that solve the problem in many instances". They do describe or link to some of these approaches (unfortunately not in ready-to-use pseudocode). I guess if you only have 10 elements in your array, an exhaustive search of all 1024 possibilities is feasible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜