Find a sum equal or greater than given target using only numbers from set

后端 未结 2 1726
独厮守ぢ
独厮守ぢ 2021-01-13 03:51

Example 1:

Shop selling beer, available packages are 6 and 10 units per package. Customer inputs 26 and algorithm replies 26, because 26 = 10 + 10 + 6.

Ex

2条回答
  •  盖世英雄少女心
    2021-01-13 04:05

    You want to solve the integer programming problem min(ct) s.t. ct >= T, c >= 0 where T is your target weight, and c is a non-negative integer vector specifying how much of each package to purchase, and t is the vector specifying the weight of each package. You can either solve this with dynamic programming as pointed out by another answer, or, if your weights and target weight are too large then you can use general integer programming solvers, which have been highly optimized over the years to give good speed performance in practice.

提交回复
热议问题