#include <iostream>
#include <limits>
int MIN = std::numeric_limits<int>::min()
using namespace std ;
void findMaxSubArray(int inputArray[] , int n )
{
int maxStartIndex=0;
int maxEndIndex=0;
int maxSum = MIN ;
int cumulativeSum= 0;
int maxStartIndexUntilNow=0;
for (int currentIndex = 0; currentIndex < n ; currentIndex++)
{
int eachArrayItem = inputArray[currentIndex];
cumulativeSum+=eachArrayItem;
if(cumulativeSum>maxSum)
{
maxSum = cumulativeSum;
maxStartIndex=maxStartIndexUntilNow;
maxEndIndex = currentIndex;
}
else if (cumulativeSum<0)
{
maxStartIndexUntilNow=currentIndex+1;
cumulativeSum=0;
}
}
cout << "Max sum : "<< maxSum << "\n" ;
cout << "Max start index : "<< maxStartIndex << "\n" ;
cout << "Max end index : "<< maxEndIndex << "\n" ;
}
int main()
{
int intArr[] = {-1,3,-1,-1,-1,-1,-1,-1 } ;
//int intArr[] = {-1, 3, -5, 4, 6, -1, 2, -7, 13, -3};
//int intArr[]={-6,-2,-3,-4,-1,-5,-5};
findMaxSubArray(intArr,8);
return 0 ;
}
我对给定here的实现是否正确持怀疑态度,因此我在C ++中完全实现了它,而对于上述测试用例,它并不起作用。我无法找出算法出错的地方?
答案 0 :(得分:1)
取
int maxSum = -1;
可以解决您的问题。
你的上述程序也不可编译。
这适用于integer
个数字
#include <iostream>
#include <limits>
using namespace std ;
int MIN = std::numeric_limits<int>::min();
void findMaxSubArray(int inputArray[] , int n )
{
int maxStartIndex=0;
int maxEndIndex=0;
int maxSum = -1 ;
int cumulativeSum= 0;
int maxStartIndexUntilNow=0;
for (int currentIndex = 0; currentIndex < n ; currentIndex++)
{
int eachArrayItem = inputArray[currentIndex];
cumulativeSum+=eachArrayItem;
if(cumulativeSum>maxSum)
{
maxSum = cumulativeSum;
maxStartIndex=maxStartIndexUntilNow;
maxEndIndex = currentIndex;
}
else if (cumulativeSum<0)
{
maxStartIndexUntilNow=currentIndex+1;
cumulativeSum=0;
}
}
cout<< "Max sum : "<< maxSum << "\n" ;
cout<< "Max start index : "<< maxStartIndex << "\n" ;
cout<< "Max end index : "<< maxEndIndex << "\n" ;
}
int main()
{
int intArr[] = {-1,3,-1,-1,-1,-1,-1,-1 } ;
//int intArr[] = {-1, 3, -5, 4, 6, -1, 2, -7, 13, -3};
//int intArr[]={-6,-2,-3,-4,-1,-5,-5};
findMaxSubArray(intArr,8);
return 0 ;
}
答案 1 :(得分:0)
int maxStartIndex=0;
int maxEndIndex=0;
int maxSum = MIN;
这是你的问题。你在骗算。在索引0处开始和结束的子数组的总和为arr[0]
,而不是负无穷大。但这也不是一个好的起点。
int maxStartIndex=0;
int maxEndIndex=-1;
int maxSum = 0;
任何数组都有一个零和子数组:一个空数组。你需要击败那个,而不是任何负数。
答案 2 :(得分:0)
总的来说,那里有很多好的资源。这是a useful resource that you should look at for C++的链接。您还可以查看this resource which is where the code below originates from and which has a C implementation。这是粗略算法的伪代码:
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
return max_so_far
这是一个在C中实现算法的简单程序:
#include<stdio.h>
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
if(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]);
int max_sum = maxSubArraySum(a, n);
printf("Maximum contiguous sum is %d\n", max_sum);
getchar();
return 0;
}
正如人们所指出的,这种方法并不适用于所有负数。如果所有数字都是负数,它只返回0。因此,我们可以进一步优化问题。下面是一些示例代码,可以很好地优化原始方法:
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
/* Do not compare for all elements. Compare only
when max_ending_here > 0 */
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
答案 3 :(得分:0)
您的代码存在的问题是(cumulativeSum&gt; maxSum)在此之前检查(累积&lt; 0)并且您的maxSum是MIN,因此如果第一个数字是负数而第二个数字是正数,则它将作为cumulativeSum&gt;失败。 maxSum so -1将被添加到cumulativeSum,因此答案将是2而不是3.因此要么检查(cumulativeSum&lt; 0)之前或者使maxSum = -1或添加条件(cumulativeSum&gt; maxSum&amp;&amp; cumulativeSum&gt; 0 )