【问题标题】:Print SubArray of Maximum Contiguous product in Array打印数组中最大连续乘积的子数组
【发布时间】:2020-09-18 19:43:51
【问题描述】:

最大乘积子数组 给定一个包含正整数和负整数的数组,找到最大积的子数组。 例子:

输入:arr[] = {6, -3, -10, 0, 2} 输出:子数组是 {6, -3, -10}

输入:arr[] = {-1, -3, -10, 0, 60} 输出:子数组是 {60}

输入:arr[] = {-2, -3, 0, -2, -40} 输出:子数组是 {-2, -40}

注意:查找最大乘积是使用 kadane 算法完成的,我尝试重用它来查找子数组,但无法破坏查找起始索引的部分,查找结束索引很容易。结束索引是 res

public int maxProductSubArray(int arr[], int len) {

        int res = arr[0];

        int max = arr[0];
        int min = arr[0];

        for (int i = 1; i < len; i++) {

            int temp = max;

            max = Math.max(Math.max(max * arr[i], min * arr[i]), arr[i]);
            min = Math.min(Math.min(temp * arr[i], min * arr[i]), arr[i]);

            res = Math.max(res, max);
        }

        return res;

    }

但无法解决问题以找到子数组。

【问题讨论】:

  • 欢迎来到 Stackoverflow,请阅读How To Ask。特别关注How To Create MCVE。确保使用适当的标签(编程语言、相关技术等)标记您的问题。您在发布一个好问题上付出的努力越多:一个易于阅读、理解并且是 on topic 的问题 - 它吸引相关人员的机会就越高,您将更快地获得帮助。祝你好运!
  • 你能建议我发布的内容有什么问题以及我错过了什么吗?标签和问题陈述很清楚。是的,我在发布这个问题之前搜索了堆栈和谷歌。
  • 现在我明白了。已经编辑并放置了我尝试过的东西以及我被卡住的地方
  • 只是建议。
  • 请查看上面链接中提供的解决方案。您只需进行一些小的更改即可实现您的目标。

标签: java arrays data-structures


【解决方案1】:

有几种可能的方法来解决这个问题:

  • 蛮力
  • 分而治之
  • 回溯

我只会向您展示蛮力解决方案。这不是最快的解决方案,但在我看来是最清晰易懂的解决方案:

public class SubArray {

    public static int findMax(int[] num) {

        int max = num[0];
        int start = 0;
        int end = 0;
        for(int i = 0; i < num.length; i++) {   //Iterating over every possible starting position
            for(int j = i; j < num.length; j++) { //Iterating over every possible end position
                int multiply = 1;
                for(int k = i; k <= j; k++) {   //Multiply from start to end
                    multiply *= num[k];
                }
                if(max < multiply) {    //Did we find a new maximum?
                    max = multiply;
                    start = i;
                    end = j;
                }
            }
        }

        System.out.print("The subarray is {");  //Print subarray with highest product
        for(int i = start; i <= end; i++) {
            System.out.print(num[i]);
            if(i < end) {
                System.out.print(", ");
            }
        }
        System.out.println("}");

        return max;
    }

    public static void main(String[] args) {
        int[] array = {6, -3, -10, 0, 2} ;
        int max = findMax(array);
        System.out.println("Maximal product is: " + max);
    }   
}

【讨论】:

    【解决方案2】:

    这个问题可以通过分而治之的方式有效解决。

    假设您要解决子数组[l, r] 的问题;然后,假设c = (l + r) / 2 解是[l, c][c + 1, r] 中的子数组,或包含cc + 1 的某个子数组。

    然后我们定义一个函数f(l, r)返回子段的答案;然后,要计算这个函数,首先递归调用f(l, c)f(c + 1, r) 并选择最大值作为临时答案。然后计算段的乘法 [c, c],然后是 [c - 1, c] 等等(使用 multiplication of [c - k, c] = multiplication of [c - k + 1, c] * array[c - k])并计算所有这些段的最大和最小乘法。对c[c + 1, c + 1][c + 1, c + 2] 等)右侧的段执行相同操作然后,答案将是一个临时答案,即最大值相乘或最小值相乘或最小值和最大值相乘,反之亦然(如果这种乘法为负,则需要最小乘以最大)。返回这四个值的最大值或作为函数结果的临时答案。

    如果有必要,除了返回乘法函数的值之外,还可以返回达到这些值的段。

    此解决方案使用Θ(n log n) 时间和Θ(n) 空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 2020-08-30
      • 1970-01-01
      • 2020-06-24
      • 2021-01-21
      • 2020-01-22
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多