简体   繁体   English

最大总和

[英]Maximum sub-sum

Here is example of my input data: 这是我的输入数据的示例:
5 // Number of 1D arrays, in this case we'll have array[5][3] 1 2 3 // Values of array[i][0..2] 1 1 1 1 0 0 1 1 0 2 1 0

And output is: 输出为:
12 // Maximum sum ({1 2 3} + {1 1 1} + {2 1 0} = {4 4 4} = 12) - END SUM VALUES MUST BE EQUAL(4 4 4). 3 // Number of arrays used to get this sum

The problem is to find maximum sum using n arrays, and secod condition is to use minimum number of arrays. 问题是使用n数组查找最大和,第二条件是使用最少数量的数组。 Also if sum > 300 we stop algorithm. 同样,如果sum > 300我们将停止算法。 (300 is maximum). (最大为300)。 Here is my code, it's I get good answers but it's time complexity is O(2^n-1). 这是我的代码,可以得到很好的答案,但是时间复杂度是O(2 ^ n-1)。 I'm thinking that it's possible to save results in some way and don't calculate same things many times, but I don't know how yet. 我认为可以以某种方式保存结果并且不必多次计算相同的结果,但是我还不知道如何。

public static int[] fuel(int start, int[] sum, int counter) {
    int[] val = { sum[0] + crystal[start][0], sum[1] + crystal[start][1], sum[2] + crystal[start][2] };
    int newSum = val[0] + val[1] + val[2];

    if(newSum > 300) 
        return null;

    if(val[0] == val[1] && val[1] == val[2]) { // All 3 values have to be equal!
        if(newSum > result[0]) {
            result[0] = newSum;
            result[1] = counter;
        } else if(newSum == result[0] && result[1] > counter) {
            result[1] = counter;
        }
    }

    if(start + 1 < crystalNumber) {
        fuel(start + 1, val, counter + 1);
        fuel(start + 1, sum, counter);
    }

    return result;
}

This may not be the best algorithm to solve this but it should be quicker than O(2^N). 这可能不是解决此问题的最佳算法,但它应比O(2 ^ N)更快。

The idea is to record all reachable sums as you loop through the input array. 这个想法是记录遍历输入数组时所有可及的总和。 You can use a dictionary whose key is a unique hash of the possible sums, for the sake of simplicity let's just assume the key is a string which concatenates the three sums, for example, the sums [3,5,4] we'll use the key "003005004" , the value of the dictionary will be the minimum numbers of arrays to reach that sum. 您可以使用一个字典,其键是可能的和的唯一哈希,为简单起见,我们假设键是一个将三个和连接起来的字符串,例如,和[3,5,4]使用键“ 003005004”,字典的值将是达到该总和的最小数组数。

So in your case: 因此,在您的情况下:

1 2 3 => [001002003] =1
1 1 1 => [001001001] =1, [002003004]=2  (itself and use [001002003] from above)
1 0 0 => [001000000] =1, [002002003] =2, [002001001] =2, [003003004] =3
1 1 0 ...
2 1 0 ...

In the end, you will find [004004004] =3 and that's your answer. 最后,您会发现[004004004] = 3,这就是您的答案。

This may seems going through all combinations as well so why it's quicker, it's because the maximum sum is 300 for each number, so in the very worst case, we may have 301^3 keys filled and have to update their values for each new input array. 这似乎也可以遍历所有组合,所以为什么会更快,这是因为每个数字的最大和为300,所以在最坏的情况下,我们可能需要填充301 ^ 3个键,并且必须为每个新输入更新它们的值阵列。 This is however still O(n) and despite of the large constant, it should still run much faster than O(2^n). 但是,它仍然是O(n),尽管常数很大,但它的运行速度仍应比O(2 ^ n)快得多。 (If you solve 300^3*n = 2^n, n is around 30-ish) (如果您求解300 ^ 3 * n = 2 ^ n,则n约为30-ish)

A simple hash function would be a*301*301+b*301+c 一个简单的哈希函数将是a * 301 * 301 + b * 301 + c

I think the problem is given m 1-D arrays and a number n , find the maximum sum using n arrays from m ; 我认为问题是给定m一维数组和一个数n ,使用m n数组找到最大和;

The solution looks straight forward. 解决方案直截了当。 keep sum of each 1-D array in a separate array, say sum[] 将每个一维数组的和保存在单独的数组中,例如sum[]

1 2 3 = 6
1 1 1 = 3
1 0 0 = 1
1 1 0 = 2
2 1 0 = 3

Sort this array sum 排序此数组sum

6,3,3,2,1

and return the sum of first n elements of this array. 并返回此数组的前n元素的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM