如何为给定的数组找到大小> gt的最大子阵列?

时间:2016-03-17 16:22:21

标签: java arrays algorithm data-structures dynamic-programming

给定一个数组和一个整数k,找到size >=2的每个连续子数组的最大值。

INPUT:

int[] array = new int[]{-5,-2,-3,-1,-1};  
int[] array1 = new int[]{5,2,3,-3,1,1};   

输出:

-1,-1   //Since the maximum sum with maximum contiguous subarray can be of size 2 or greater than 2 and all are negative, so we taking the subarray  = -1 -1    
5,2,3  // Because we need to find the maximum sum and  maximum subarray with limitation that array can be >= 2  

我已经解决了这个问题,但是当所有整数都是负数时,我的解决方案无效。此外,当输入为-5,-2,-3,-3,1,1时,它不起作用。输出应为1和1,但它将为-3,-3,1和1。

我需要一个优化的解决方案。请在下面找到我的代码:

public class Pro {

    static int[] maxSubarray(int[] a) {

        int max_sum = a[0];
        int curr_sum = a[0];

        int max_start_index = 0;
        int startIndex = 0;
        int max_end_index = 1;

        for (int i = 1; i < a.length; i++) {

            if (max_sum > curr_sum + a[i]) {
                startIndex = i-1;
                curr_sum = a[i];
            } else {
                curr_sum += a[i];
            }

            if (curr_sum > max_sum) {
                max_sum = curr_sum;
                max_start_index = startIndex;
                max_end_index = i;
            }
        }

        if (max_start_index <= max_end_index) {
            return Arrays.copyOfRange(a, max_start_index, max_end_index + 1);
        }

        return null;
    }

    public static void main(String[] args) {

        int[] array = new int[]{-5,-2,-3,-1,-1};
        int[] array1 = new int[]{5,2,-3,1,1};
        int[] out = maxSubarray(array1);
        for(int a : out)
        {
            System.out.println(a);
        }

    }

}

还有一个代码片段可以提供帮助。它找到了最大的子阵列。请参阅以下代码:

public int maxSubArray(int[] A) {
        int max = A[0];
        int[] sum = new int[A.length];
        sum[0] = A[0];

        for (int i = 1; i < A.length; i++) {
            sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
            max = Math.max(max, sum[i]);
        }

        return max;
    }

1 个答案:

答案 0 :(得分:0)

您使用的算法称为Kardane算法,当所有数字都为负时,不能使用。至少有一个数字必须为正才能使算法工作。要使其适用于具有负数但至少有一个正数的数组,每当遇到数组中的负数时,curr_sum应设置为0。实现此目的的一种方法是使用自定义max-function

private int max(int first, int second) {
     if (first < second) return first;
     else return second;
}

替换:

if (max_sum > curr_sum + a[i]) {
       startIndex = i-1;
       curr_sum = a[i];
       } else {
          curr_sum += a[i];
       }

使用:

curr_sum = max(curr_sum + a[i], 0); 
if (curr_sum == 0) startIndex = i + 1; 

完整代码:

   public int max(int[] a) {
    int max_sum = a[0];
    int curr_sum = a[0];
    int startIndex = 0;
    int max_start_index = 0;
    int max_end_index = 1;
    for (int i = 1; i < a.length; i++) {
        max_sum = max(max_sum + a[i], 0);
        if (max_sum == 0) startIndex = i + 1;
        curr_sum = max(curr_sum, max_sum);
        if (curr_sum == max_sum) {
            max_start_index = startIndex;
            max_end_index = i;
        }
    }
    System.out.println("Start index: " + max_start_index);
    System.out.println("End index: " + max_end_index);
    return curr_sum;
}

输入:-5, -2, -3, -3, 1, 1

输出:

Start index: 4 End index: 5 2