How to split a list of items into equal partitions according to the item's weight?

后端 未结 3 1663
北海茫月
北海茫月 2021-01-04 10:06

I have a list of items that is somewhat like this:

[
  [\"orange\", 9],
  [\"watermelon\", 3],
  [\"grapefruit\", 6],
  [\"peach\", 8],
  [\"durian\", 2],
           


        
3条回答
  •  感动是毒
    2021-01-04 10:36

    You could total all the weights, divide by the number of groups, come up with a target weight, and then iterate through the items in descending weight order tossing them into the same group if they fit at or under the target weight, and into the other group if they don't.

    There's probably some math dr.s out there who can come up with a formal proof of how best to do it, but that was my thought off the top of my head.

提交回复
热议问题