需要编写一个算法,用于从每个Array 2值的Array 1值中获取值的总和

时间:2015-09-01 10:24:11

标签: javascript arrays

我正在创建一个算法,将第一个数组的单元格的任意组合与第二个数组中的第二个数组值匹配。例如在javascript中:

var arr=[10,20,30,40,50,60,70,80,90];
var arr2=[100,120,140];

我想要的是自动定义为以下逻辑(第二个数组的单元格值的优先级),请帮我找到伪算法

100 = 10+20+30+40 //arr2[0] = arr1[0] + arr1[1] + arr1[2] + arr1[3]
120 = 50+70 //arr2[1] = arr1[4] + arr1[6]
140 = 60+80 //arr2[2] = arr1[5] + arr1[7]
90 = 90  //remaining arr1[8]

值是demo,可以动态更改。

2 个答案:

答案 0 :(得分:0)

如果你将两个数组作为排序数组,然后开始从第一个数组(array1)的最后一端添加元素,这是最大的,因为数组是排序的,现在检查sum是否匹配然后如果sum小于你正在检查array2中的元素,然后你需要从array1添加第三个元素。另一种情况是,如果sum大于array2中的元素,那么你必须忽略你另外使用的array1中的一个元素,并用你在第一个数组中使用的前一个元素替换。重复这些步骤。你需要考虑如何正确地做到这一点,否则你需要分享你的一些工作或逻辑,以便我们可以提供帮助

答案 1 :(得分:0)

As the matter is quite complex, over and above sufficing on a pseudo code style explanation, I have also coded a practical implementation that you may find at this link.

I advise you to refrain from looking at the solution and first try to implement the algorithm yourself as there is a lot of scope for further improvement.

Here is in broad lines an explanation to the way I have decided to tackle the algorithm:

The problem presented by the OP is related to a classic example of distributing n unique elements over k unique boxes. In this case here, arr has 9 unique elements that need to be distributed over three distinct spots, represented by the container: arr2. So the first step in tackling this problem is to figure out how you can implement a function that given n and k, is able to calculate all the possible distributions that apply.

The closest that I could come up with was the Stirling Numbers of the Second Kind, which is defined as:

The number of ways of partitioning a set of n elements into m nonempty sets (i.e., m set blocks), also called a Stirling set number. For example, the set {1,2,3} can be partitioned into three subsets in one way: {{1},{2},{3}}; into two subsets in three ways: {{1,2},{3}}, {{1,3},{2}}, and {{1},{2,3}}; and into one subset in one way: {{1,2,3}}.

If you pay close attention to the example provided, you will realize that it pertains to the enumeration of all the distribution combinations possible over INDISTINGUISHABLE partitions as order doesn't matter. Since in our case, each spot in the container arr2 represents a UNIQUE spot and order therefore does matter, we will thus be required to enumerate all the Stirling Combinations over every possible combination of arr2. Practically speaking, this means that for our example where arr2.length === 3, we will be required to apply all of the Stirling Combinations obtained to [100,120,140], [120,140,100], [140,100,120] etc.(in total 6 permutations)

The main challenging part here is to implement the Stirling Function, but luckily somebody has already done so: http://blogs.msdn.com/b/oldnewthing/archive/2014/03/24/10510315.aspx

After copy and pasting the Stirling Function and using it to distribute arr over 3 unique spots, you now need to filter out the distributions that don't sum up to the designated spots encompassed by arr2.

This will then leave you with all the possible solutions that apply. In your case, for var arr=[10,20,30,40,50,60,70,80,90]; var arr2=[100,120,140]; no solutions apply at all.

A quick workaround to that is by expanding the distribution target arr2 from [100,120,140] to [100,120,140,90]. A better workaround is that in the case zero solutions are found, then take away one element from list arr until you obtain a solution. Then you can later on expand your solution sets by including this element where it represents a mapping of it unto itself.

相关问题