子集总和:查找总和大于K的子集

编程入门 行业动态 更新时间:2024-10-09 02:23:40
本文介绍了子集总和:查找总和大于K的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个正整数数组,比如{a1,a2,....,an},我想找出满足以下条件的所有可能的子集:

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)

其中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。 :我并不需要所有的子集,而是说形成的所有子集的所有元素的乘积。

P.S. : I don't exactly need all the subsets, but say product of all elements for all subsets formed.

推荐答案

您的问题看起来类似于0-1背包问题,除了在事物上-通常这种限制是,总和必须小于K 。

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.

因此,例如,如果找到子集,其总和大于K,则可以添加集合中任何不包含在子集中的元素,并且仍然是答案。假设您的集合是{a1,a2,a3,a4} ans sum {a1}> =K。然后,您有2 ^ 3种方式将a2,a3和a4添加到子集中。

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.

最坏的情况是,当K = 0时,并且您有N个元素大于0。因此,您需要列出2 ^ N个子集。

Worst case is, when K = 0. And you have N elements greater than 0. So, you need to list 2 ^ N subsets.

可能,此链接可以帮助 en.wikipedia/wiki/Subset_sum_problem

Probably, this link could help en.wikipedia/wiki/Subset_sum_problem

更多推荐

子集总和:查找总和大于K的子集

本文发布于:2023-11-29 15:48:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子集   总和

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!