divide list in two parts that their sum closest to each other

前端 未结 3 1278
深忆病人
深忆病人 2020-12-09 12:56

This is a hard algorithms problem that :

Divide the list in 2 parts (sum) that their sum closest to (most) each other

list length is 1 <

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 13:46

    You can reformulate this as the knapsack problem.

    You have a list of items with total weight M that should be fitted into a bin that can hold maximum weight M/2. The items packed in the bin should weigh as much as possible, but not more than the bin holds.

    For the case where all weights are non-negative, this problem is only weakly NP-complete and has polynomial time solutions.

    A description of dynamic programming solutions for this problem can be found on Wikipedia.

提交回复
热议问题