java - 如何找到从唯一数组索引提取的数组中数字的最大可能总和

标签 java arrays

我需要找到数组中数字的最大可能总和,但数字必须从唯一的数组索引中提取......就像这样:

double maxSum = 0;

double a = {1.0 , 2.0, 3.0};
double b = {4.0 , 5.0, 6.0};
double c = {7.0 , 8.0, 9.0};

sum = a[0] + b[1] + c[2];

// or sum = a[0] + b[2] + c[1]
// or sum = a[1] + b[0] + c[2]
// etc. - how to do that for i arrays and for j numbers in array?

if(sum >=maxSum){
maxSum = sum;
} 

这就是我所做的 - 但不知道下一步该做什么...

    public ArrayList<Double[]> tempArrayCreator() {
    ArrayList<Double[]> lists = new ArrayList<Double[]>();

    Double[] list1 = { 6.0, 7.0, 6.0 };
    Double[] list2 = { 4.5, 6.0, 6.75 };
    Double[] list3 = { 6.0, 5.0, 9.0 };

    lists.add(list1);
    lists.add(list2);
    lists.add(list3);

    return lists;
}

public double maxPossibleSum(ArrayList<Double[]> lists) {

    double result = 0.0;

    for (int i = 0; i < lists.size(); i++) {
        for (int j = 0; j < lists.get(i).length; j++) {

        // ???

        }
    }

    return result;

}

编辑。示例:

list1 = { 1, 5, 2};
list2 = { 9, 3, 7};
list3 = { 8, 4, 9};

possible solutions:
list1[0] + list2[1] + list3[2] = 13
list1[0] + list2[2] + list3[1] = 12
list1[1] + list2[0] + list3[2] = 23 <-- here it is!
list1[1] + list2[2] + list3[0] = 20
list1[2] + list2[0] + list3[1] = 15
list1[2] + list2[1] + list3[0] = 13

最佳答案

首先,您想要获取索引的所有排列的列表。

{[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 1, 0], [2, 0, 1]}

例如,这个answer给出了一种方法:

public static List<List<Integer>> getAllPermutations(int arraySize) {
    List<Integer> elements = new ArrayList<Integer>();
    for (int i = 0; i < arraySize; i++) {
        elements.add(i);
    }
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    getAllPermutations(result, elements, 0);
    return result;
}

private static void getAllPermutations(List<List<Integer>> result, List<Integer> elements, int k) {
    for (int i = k; i < elements.size(); i++) {
        java.util.Collections.swap(elements, i, k);
        getAllPermutations(result, elements, k + 1);
        java.util.Collections.swap(elements, k, i);
    }
    if (k == elements.size() - 1) {
        result.add(new ArrayList<Integer>(elements));
    }
}

然后迭代所有排列:

public double maxPossibleSum(ArrayList<Double[]> lists) {
    List<List<Integer>> allPermutations = getAllPermutations(3);
    double maxSum = Double.NEGATIVE_INFINITY;
    for (List<Integer> permutation : allPermutations) {
        double sum = 0;
        for (int i = 0; i < permutation.size(); i++) {
            Integer index = permutation.get(i);
            sum += lists.get(i)[index];
        }
        if (sum > maxSum) {
            maxSum = sum;
        }
    }
    return maxSum;
}

关于java - 如何找到从唯一数组索引提取的数组中数字的最大可能总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644020/

相关文章:

javascript - 如何计算 JavaScript 数组中的字符数?

java - 如何忽略一个实体中的属性而不忽略另一个实体中的属性 - JSON

java - 检查 Java 应用程序的多个实例是否正在运行(不阻止它们)

Java程序延迟空白字符串从打印到控制台

Java 8 lambda 聚合

javascript - 从中心位置旋转数组

arrays - 当我尝试将元素放入单元格时,为什么我的分割数组会抛出错误?

c++ - 如何取消引用数组元素 (C++)

用于 eclipse 的 Java 安装程序生成器(插件)

python - 从两个列表中获取匹配元素数量的最佳方法是什么 - 假设两个列表中都有重复项?