How to divide a set into two subsets such that difference between the sum of numbers in two sets is minimal?

前端 未结 18 729
攒了一身酷
攒了一身酷 2020-11-27 10:55

Given a set of numbers, divide the numbers into two subsets such that difference between the sum of numbers in two subsets is minimal.

T

18条回答
  •  被撕碎了的回忆
    2020-11-27 11:47

    Are you sorting your subset into decending order or ascending order?

    Think about it like this, the array {1, 3, 5, 8, 9, 25}

    if you were to divide, you would have {1,8,9} =18 {3,5,25} =33

    If it were sorted into descending order it would work out a lot better

    {25,1}=26 {9,8,5,3}=25

    So your solution is basically correct, it just needs to make sure to take the largest values first.

    EDIT: Read tskuzzy's post. Mine does not work

提交回复
热议问题