我最近遇到了一个问题,我需要弄清楚如何将项目分发到桶中,但我需要找到所有分配方法。桶之间的所有项目分布
输入以整数数组的形式出现,它告诉您每列可以容纳的最大值,并且数组中必须有N个项目。
例如:
maxItems = 3
maximums = [4,2,1] # The order of maximums DOES matter meaning
# This means that the results of maximums = [2,4,1] are different from maximums = [1,2,4]
outputs = [[3,0,0],[2,1,0],[1,1,1],[2,0,1],[0,2,1]] # results are in no particular order
# notice how the sum of each result is equal to maxItems and each value in each of the rows are less than the value inside of maximums
我试图解决在JavaScript这个问题,但我无法弄清楚如何解决这个问题。我想首先填写尽可能多的数字并开始向右移动,但随着最大值数组变大,此方法变得更加不准确,我完全不知道如何处理它。
如果您还有其他问题,请随时询问您是否不了解问题。
我开始了与该代码在JavaScript是
var all_combinations = function(N, maximums){
var empty = maximums.map(function(){return 0;}); // create empty array size of maximums filled with 0s
var s = 0;
for (var i = 0; i < empty.length && s < N;){
if (empty[i] >= maximums[i]){i++;continue;}
empty[i]++;
s++;
} // fill the left side with as many items as possible
// Then i would proceed to move one item at a time to the right side but some how i would need to do it for the whole array and this is where I get stuck.
};
我试着搜索了这个问题,但我从来没有发现如何做到这一点它是在这里设立的方式。我试着找到类似的问题,但他们总是与此无关。也许我正在寻找错误的问题。如果有人可以链接一个很有用的资源。
如果您有任何问题,请询问他们。我会尽我所能回答。
我失去了一些信息,以使这是一个可以接受的问题吗?已经有一次近距离投票,甚至不到5分钟 – ahitt6345
不,你不是。我个人不同意投票结束这个,这只是一个有点计算机科学的重要问题,而且大多数问题都很简单,“请帮助解决我的计划”这种交易,所以这个问题需要更长的时间才能回答。不过,不用担心,你是在正确的地方,我认为这是一个深思熟虑的问题。 –
如果我理解你,你想在'maximums'所定义的边界内找到所有可能的排列,并将它们按'maxItems'或'N'定义的大小进行分组。我对么? – Xiaoy312