问题
I have an array of positive integers say {a1, a2, ...., an}, and I want to find out all possible subsets of the array which satisfy the following condition:
(sum >= K)
where K is a positive integer. I know the solution for this problem is dynamic programming, but unable to think how to use it for this case. Please help.
P.S. : I don't exactly need all the subsets, but say product of all elements for all subsets formed.
回答1:
Your problem looks similar to 0-1 Knapsack problem, except on thing - usually this restriction is, that sum must be smaller than K.
But you list the problem, where restriction is, that sum must be bigger.
So, for example, if you find subset, sum of it is bigger than K, then you could add any element of set, that not included in your subset and this is still the answer. Let's assume, your set is {a1, a2, a3, a4} ans sum {a1} >= K. Then, you have 2^3 ways to add a2, a3 and a4 to your subset.
So, this problem looks like exponential problem, since you need to list all possible variations.
Worst case is, when K = 0. And you have N elements greater than 0. So, you need to list 2 ^ N subsets.
Probably, this link could help http://en.wikipedia.org/wiki/Subset_sum_problem
来源:https://stackoverflow.com/questions/18125393/subset-sum-find-subset-whose-sum-greater-than-k