【问题标题】:Numbers which constitute the Maximum sum构成最大和的数字
【发布时间】:2019-05-20 10:51:24
【问题描述】:

我刚刚编写了从数组中找到最大和的程序, 但我被困在了有什么方法可以找到哪些数字对最大总和有贡献?

给出了最大和的规则:没有相邻元素应该贡献 总结。

我对数组中最大和的解决方案:

public class MaximumELementInARray {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String[] al = reader.nextLine().split(" ");
        int[] input = Arrays.stream(al).mapToInt(Integer::parseInt).toArray();
        MaximumELementInARray mm = new MaximumELementInARray();
        int maxi = mm.maximumm(input);
        System.out.println(maxi);
    }

    public int maximumm(int[] a) {
        List<Integer> ex = new ArrayList<>();
        List<Integer> inc = new ArrayList<>();
        int incl = a[0];
        int excl = 0;
        int excl_new;
        for (int i = 1; i < a.length; i++) {
            excl_new = Math.max(incl, excl);
            incl = excl + a[i];
            excl = excl_new;
        }
        System.out.println(incl > excl ? inc : ex);
        return incl > excl ? incl : excl;
    }
}

现在在maximum 函数中是否有一个调整,我可以将构成最大总和的元素的所有索引放入其中?

输入:

-1 7 8 -5 4 9 -2 3

输出:

20

**

我需要 20 是如何得出的。答案应该是8+9+3

**

我相信在最大函数中我们可以放置一个 Arraylist 并记录哪些元素对 sum 有贡献,但我无法实现。

我做了两个Arraylist:

List<Integer> ex = new ArrayList<>();
List<Integer> inc = new ArrayList<>();

输入:-1 7 8 -5 4 输出:12 和由 8+4 组成


输入:3 2 1 -1 输出:4 总和由3+1组成

等等……

【问题讨论】:

  • 先生,最大和是根据和中不应有相邻元素的规则计算的。
  • 在您的情况下,最大金额是多少?您不是说总和最大的子集吗?对于您的示例,7+8+(-5)+4+9?
  • 先生,最大总和是正确的,在我的情况下,我是否可以标记哪些元素对最大总和有贡献?
  • 您正在尝试解决一项任务,即找到与给定数字相加的子集,这在一般情况下是 NP 完全的,通常通过动态规划来解决。更好的解决方案是修改 MaximumELementInARray.maximum 方法,不仅返回结果总和本身,还返回形成它的被加数。
  • @AlexSalauyou ,我会喜欢这个解决方案和方法。

标签: java algorithm java-8


【解决方案1】:

您可以按照此代码进行操作。

    int toIndex = 3, fromIndex = 0;
    List<Integer> result = new ArrayList<>();
    while (toIndex < numbers.size()) {
        Map<Integer, Integer> map = IntStream
                .range(fromIndex, toIndex)
                .filter(i->numbers.get(i)>0)
                .mapToObj(i -> new AbstractMap.SimpleEntry<>(i, numbers.get(i)))
                .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey,(a,b)->b));
        // find max of sublist
        int maxOfSub = numbers.subList(fromIndex, toIndex).stream().max(Integer::compareTo).get();
        //update indexes
        fromIndex = map.getOrDefault(maxOfSub,toIndex-1) + 2;
        toIndex += fromIndex;

        if (maxOfSub > 0)
            result.add(maxOfSub);
    }
    int lastMax = numbers.subList(fromIndex, numbers.size()).stream().max(Integer::compareTo).get();
    if (lastMax > 0)
        result.add(lastMax);
    System.out.println(result);
    System.out.println(result.stream().reduce(0, Integer::sum));

DEMO

【讨论】:

  • @tromu 添加了演示来回答。
  • 嘿,谢谢,但输出只有 20,它不会打印构成 20 的数字,答案应该是 8,9,3 而不是 20。
  • 此外,您的程序将 9 作为 5 10 4 -1 的总和,最大总和应为 10。:(
  • 哇,所有情况都涵盖了,但至少 1 没有通过。 4 5 4 3 ,应该给出 [4,4] 或 [5,3] 但我得到 Exception in thread "main" java.lang.IllegalStateException: Duplicate key 0 ,无论如何要解决这种情况?非常感谢你。
  • 如果我想得到4 4 作为4 4 的输出也构成sum 8,那我该怎么办?
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 2021-01-24
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
相关资源
最近更新 更多