Given a target sum and a set of integers, find the closest subset of numbers that add to that target

后端 未结 3 1122
南方客
南方客 2020-12-13 20:31

I have a set of integers M and a target sum k. I want to find the subset of M that when added together is the closest to k without going over.

For example:



        
3条回答
  •  醉酒成梦
    2020-12-13 20:51

    If the target sum k is not too large, look at http://en.wikipedia.org/wiki/Subset_sum_problem#Pseudo-polynomial_time_dynamic_programming_solution - you can use this to create a bitmap which tells you which numbers can be produced using your subset. Then just pick the closest possible number to k in the bitmap.

提交回复
热议问题