这是计算的建议B
。这是一个很好的观察,它归结为“给定一个整数数组,找到所有子集,其总和为 <= m 并且我们不能从数组中添加任何其他元素使得 <= m 不被违反”。所以我只解决了这个更简单的问题,并相信你会采用适合你情况的解决方案。
正如我在评论中所说,我正在使用递归。每个递归调用都会查看 A 中的一个元素,并尝试使用该元素的解决方案和没有该元素的解决方案。
在对递归方法的每次调用中,我传递A
and m
,这些在每次调用中都是相同的。我传递了一个部分解决方案,告诉哪些先前考虑的元素包含在当前正在构建的子集中,以及包含元素的总和只是为了方便。
/**
* Calculates all subsets of a that have a sum <= capacity
* and to which one cannot add another element from a without exceeding the capacity.
* @param a elements to put in sets;
* even when two elements from a are equal, they are considered distinct
* @param capacity maximum sum of a returned subset
* @return collection of subsets of a.
* Each subset is represented by a boolean array the same length as a
* where true means that the element in the same index in a is included,
* false that it is not included.
*/
private static Collection<boolean[]> maximalSubsetsWithinCapacity(int[] a, int capacity) {
List<boolean[]> b = new ArrayList<>();
addSubsets(a, capacity, new boolean[0], 0, Integer.MAX_VALUE, b);
return b;
}
/** add to b all allowed subsets where the the membership for the first members of a is determined by paritalSubset
* and where remaining capacity is smaller than smallestMemberLeftOut
*/
private static void addSubsets(int[] a, int capacity, boolean[] partialSubset, int sum,
int smallestMemberLeftOut, List<boolean[]> b) {
assert sum == IntStream.range(0, partialSubset.length)
.filter(ix -> partialSubset[ix])
.map(ix -> a[ix])
.sum()
: Arrays.toString(a) + ' ' + Arrays.toString(partialSubset) + ' ' + sum;
int remainingCapacity = capacity - sum;
if (partialSubset.length == a.length) { // done
// check capacity constraint: if there’s still room for a member of size smallestMemberLeftOut,
// we have violated the maximality constraint
if (remainingCapacity < smallestMemberLeftOut) { // OK, no more members could have been added
b.add(partialSubset);
}
} else {
// try next element from a.
int nextElement = a[partialSubset.length];
// i.e., decide whether should be included.
// try with and without.
// is including nextElement a possibility?
if (nextElement <= remainingCapacity) { // yes
boolean[] newPartialSubset = Arrays.copyOf(partialSubset, partialSubset.length + 1);
newPartialSubset[partialSubset.length] = true; // include member
addSubsets(a, capacity, newPartialSubset, sum + nextElement, smallestMemberLeftOut, b);
}
// try leaving nextElement out
boolean[] newPartialSubset = Arrays.copyOf(partialSubset, partialSubset.length + 1);
newPartialSubset[partialSubset.length] = false; // exclude member
int newSmallestMemberLeftOut = smallestMemberLeftOut;
if (nextElement < smallestMemberLeftOut) {
newSmallestMemberLeftOut = nextElement;
}
addSubsets(a, capacity, newPartialSubset, sum, newSmallestMemberLeftOut, b);
}
在一些地方有点棘手。我希望我的评论能帮助你度过难关。否则请询问。
让我们试一试:
int[] a = { 5, 1, 2, 6 };
Collection<boolean[]> b = maximalSubsetsWithinCapacity(a, 8);
b.forEach(ba -> System.out.println(Arrays.toString(ba)));
此代码打印:
[true, true, true, false]
[false, true, false, true]
[false, false, true, true]
[true, true, true, false]
表示 5、1 和 2 的子集。总和为 8,所以这正好符合 8 的容量 ( m
)。
[false, true, false, true]
表示 1 和 6,总和为 7,我们不能加 2,否则我们会超出容量
- finally
[false, false, true, true]
表示 2 和 6,也完全符合容量m
。
我相信这会耗尽您的限制范围内的可能性。