Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用分治的Maxsub阵列_Java_Arrays_Algorithm - Fatal编程技术网

Java 使用分治的Maxsub阵列

Java 使用分治的Maxsub阵列,java,arrays,algorithm,Java,Arrays,Algorithm,我无法使用分治实现最大子数组问题 假设我有一个数组[3,6,-1,2],我想按连续顺序找到这个数组的最大和。我们可以看到,从[0,3]中求和是10 我试图实现我书中的伪代码,但答案不正确 // return (max-left, max-right, max sum left + right) public static int[] maxcross(int[] array, int low, int mid, int high) { int leftSum = -10000000;

我无法使用分治实现最大子数组问题

假设我有一个数组[3,6,-1,2],我想按连续顺序找到这个数组的最大和。我们可以看到,从[0,3]中求和是10

我试图实现我书中的伪代码,但答案不正确

// return (max-left, max-right, max sum left + right)
public static int[] maxcross(int[] array, int low, int mid, int high) {
    int leftSum = -10000000;
    int rightSum = -10000000;
    int sum = 0;
    int maxLeft=0;
    int maxRight=0;
    for(int i=mid;i<mid-low;i--){
        sum = sum + array[i];
        if(leftSum < sum){
            leftSum = sum;
            maxLeft = i;
        }
    }
    sum=0;
    for(int i=mid+1;i<high;i++){
        sum = sum + array[i];
        if(rightSum < sum){
            rightSum = sum;
            maxRight = i;
        }
    }
    int cross[] = {maxLeft,maxRight,leftSum+rightSum};
    return cross;
}

public static int[] maxsub(int array[], int low, int high){
    int[] maxSubLeft = new int[3];
    int[] maxSubRight = new int[3];
    int[] maxSub = new int[3];
    int[] maxSubCross = new int[3];
    int mid;
    if (high==low){
        maxSub[0] = low;
        maxSub[1] = high;
        maxSub[2] = array[low];
        return maxSub;
    }
    else{
        mid = (int) Math.floor((low+high)/2);
        maxSubLeft = maxsub(array,low,mid);
        maxSubRight = maxsub(array,mid+1,high);
        maxSubCross = maxcross(array,low,mid,high);

        if(maxSubLeft[2] >= maxSubRight[2] && maxSubLeft[2] >= maxSubCross[2])
            return maxSubLeft;
        else if(maxSubRight[2] >= maxSubLeft[2] && maxSubRight[2] >= maxSubCross[2])
            return maxSubRight;
        else
            return maxSubCross;
    }
}
//返回(最大左、最大右、最大和左+右)
公共静态int[]maxcross(int[]array、int-low、int-mid、int-high){
int leftSum=-10000000;
int rightSum=-10000000;
整数和=0;
int maxLeft=0;
int maxRight=0;
对于(int i=mid;i=maxSubCross[2])
返回Maxft;
else if(maxSubRight[2]>=maxsubrift[2]&&maxSubRight[2]>=maxSubCross[2])
返回maxSubRight;
其他的
返回maxSubCross;
}
}
我得到这个作为输出

一,

一,

六,


有人能帮我吗?

在maxsub(…)中递归初始输出错误,
高=低和
数组[low]<0时返回0

public static int[] maxsub(int array[], int low, int high){
    int[] maxSubLeft = new int[3];
    int[] maxSubRight = new int[3];
    int[] maxSub = new int[3];
    int[] maxSubCross = new int[3];
    int mid;
    if (high==low){
        maxSub[0] = low;
        maxSub[1] = high;
        maxSub[2] = array[low];
        return maxSub; // if (array[low] < 0) return 0;
    }
    else{
        mid = (int) Math.floor((low+high)/2);
        maxSubLeft = maxsub(array,low,mid);
        maxSubRight = maxsub(array,mid+1,high);
        maxSubCross = maxcross(array,low,mid,high);

        if(maxSubLeft[2] >= maxSubRight[2] && maxSubLeft[2] >= maxSubCross[2])
            return maxSubLeft;
        else if(maxSubRight[2] >= maxSubLeft[2] && maxSubRight[2] >= maxSubCross[2])
            return maxSubRight;
        else
            return maxSubCross;
    }
}
公共静态int[]maxsub(int数组[],int低,int高){
int[]maxSubLeft=新int[3];
int[]maxSubRight=新int[3];
int[]maxSub=新的int[3];
int[]maxSubCross=新int[3];
int mid;
如果(高==低){
maxSub[0]=低;
maxSub[1]=高;
maxSub[2]=阵列[低];
返回maxSub;//如果(数组[low]<0)返回0;
}
否则{
中间=(内部)数学层((低+高)/2);
maxSubLeft=maxsub(阵列、低、中);
maxSubRight=maxsub(阵列,中+1,高);
maxSubCross=maxcross(阵列、低、中、高);
如果(maxSubLeft[2]>=maxSubRight[2]&&maxSubLeft[2]>=maxSubCross[2])
返回Maxft;
else if(maxSubRight[2]>=maxsubrift[2]&&maxSubRight[2]>=maxSubCross[2])
返回maxSubRight;
其他的
返回maxSubCross;
}
}
顺便说一下,您的递归算法是O(NlnN),一个更有效且易于实现的算法是O(N),它应用了动态规划

public static int maxSum(int array[], int low, int high) {
   int maxsum = 0, maxleftsum = 0;
   for (int i = low; i < high; i++) {
       maxsum = max(maxsum, array[i] + maxleftSum);
       maxleftSum = max(0, maxleftSum+array[i]);
   }
   return maxsum; // return the index if necessary. 
}
公共静态整数最大和(整数数组[],整数低,整数高){
int maxsum=0,maxleftsum=0;
对于(int i=低;i<高;i++){
maxsum=max(maxsum,数组[i]+maxleftSum);
maxleftSum=max(0,maxleftSum+数组[i]);
}
return maxsum;//必要时返回索引。
}