开发者

Fair Sorting two arrays by values in another array

开发者 https://www.devze.com 2023-03-31 15:17 出处:网络
In ruby, I need to sort an array that contains ten hashes, in to two arrays. Each hash has an entry named \"points\"

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

精彩评论

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