窗口k算法的最大和子数不能正确返回起始索引

时间:2016-03-24 17:48:55

标签: java arrays algorithm sub-array

这是我的maxSum子阵列的代码

给定输入数组 - > [110,-4,3,6,7,11]和k = 3,代码应该给出[110,-4,3],因为那是具有最大总和的子阵列。

我已经实现了算法。它适用于除一个以外的所有输入。我无法弄清楚出了什么问题。

public class MaxSumSizeK {


        private static int getMaxAvgSubarrayStartIndex(int input[], int k)
        {
            int n = input.length;
            if (k > n)
                throw new IllegalArgumentException("k should be less than or equal to n" );      
            if(k == n) {
                return 0;    
            }

            int sum = input[0];
            for (int i = 1; i < k; i++)
                sum += input[i];

            int maxSum = sum;
            int maxSumIndex = 0;

            for (int i = k; i < n; i++){
                sum = sum - input[i-k] + input[i] ;
                if (sum > maxSum){
                    maxSum = sum;
                    maxSumIndex = i-k;
                }
            }
            return maxSumIndex+1;
        }

        public static void printMaxAvgSubarray(int[] input, int k) {
            System.out.print("Maximum average subarray of length "  + k + " is:  " );
            int index = getMaxAvgSubarrayStartIndex(input, k);
            for(int i =0 ; i < k; i++) {
                System.out.print(input[index++] + " " );
            }
        }

        public static void main(String[] args) {
            int[] input = {11, -8, 16, -7, 24, -2, 300};
            int k = 3;
            printMaxAvgSubarray(input, k);
            System.out.println();
            int[] input1 = {110, -8, 16, -7, 24, -2, 3};
            printMaxAvgSubarray(input1, k);
            System.out.println();
            int[] input2 = {11, -8, 16, -7, 24, -2, 3};
            printMaxAvgSubarray(input2, k);
            System.out.println();
        }
    }

这是输出:

Maximum average subarray of length 3 is:  24 -2 300 
Maximum average subarray of length 3 is:  -8 16 -7 
Maximum average subarray of length 3 is:  16 -7 24 

对于input1,我没有看到应该是“110,-8,16”的预期答案。我尝试将return语句更改为“maxSumIndex”而不是“maxSumIndex + 1”。打破了其他两个输入。

请提供你的文件

2 个答案:

答案 0 :(得分:1)

有两个错误:

  1. maxSumIndex = i-k;应该是maxSumIndex = i-k+1; i-k - 它是前一个数组的第一个索引,而不是当前最大数组
  2. return maxSumIndex+1;在索引0的情况下应该是return maxSumIndex;(即前三项是最大的) - 你将返回1,这是错误的

答案 1 :(得分:1)

试试这个:

        int n = input.length;
        if (k > n)
            throw new IllegalArgumentException("k should be less than or equal to n" );
        if (k < 1)
            throw new IllegalArgumentException("k should be greater than 0" );      
        if(k == n) {
            return 0;    
        }

        int sum = 0;
        for (int i = 0; i < k; i++)
            sum += input[i];

        int maxSum = sum;
        int maxSumIndex = 0;

        for (int j = 1; j < n-k; j++){
            sum = 0;
            for (int i = 0; i < k; i++){
                sum = sum + (int) Math.abs(input[i+j]);
            }
            if (sum > maxSum){
                maxSum = sum;
                maxSumIndex = i-k;
            }
        } 
        return maxSumIndex;