简体   繁体   中英

Can someone please explain this algorithm in C++?

Quadratic Maximum contiguous subsequence sum algorithm

int maxSubSum2( const vector<int> & a)
{
  int maxSum = 0;
  for (int i = 0; i< a.size(); ++i)
  {
    int thisSum = 0;
    for (int j = i; j < a.size(); ++j)
    {
      thisSum += a[j];
      if (thisSum > maxSum)
        maxSum = thisSum;
    }
  }
  return maxSum;
}

I was wondering if anyone can explain how the algorithm works? I am good with for loops, I'm just bad at nested ones. Is "thisSum" always 0 every time the outer for loop on line 8 runs or is it static?

Thank you very much! I am trying really hard to understand the algorithm. Please help me! I really appreciate the time and effort.

The outer loop iterates over every element of the vector a . On each iteration, i will be the index of the current element, it resets thisSum to 0 , and it then executes the inner loop.

The inner loop iterates over every element starting from i . On each iteration, j will be the index of its current element. It then calculates the sum of these elements in thisSum .

The outer loop replaces maxSum with thisSum if it's higher than what it already contains.

So if the vector contains:

1 7 -10 2 4

the successive iterations of the outer loop will calculate the following values of thisSum :

1 + 7 + -10 + 2 + 4 = 4
7 + -10 + 2 + 4 = 3
-10 + 2 + 4 = -4
2 + 4 = 6
4 = 4

The first iteration it will set maxSum to 4 . After the 2nd and 3rd iterations, thisSum > maxSum will be false, so it won't change it. On the 4th iteration, 6 > 4 , so it will set maxSum to 6 . The last iteration won't change it. Finally, it will return 6 .

Every time the outer for loop loops, this sum is reset to 0 because of the =0 that is on the first line of the outer loop.

I suggest you modify your function to print i, j, and thisSum in the inner loop so that you can see how they are changing.

Example a = [1, 2, 3, 4, 5]

j starts at the value of i, so it will first start at 0, then 1, then 2 and so on. Thus this second, inner loop is smaller each time the outer loop increments.

thisSum is reset to 0 each time as it is NOT static. If it was, it would be labeled static.

Basically, in this algorithm the outer loop is used to push the 'starting index' forward, with the inner loop used to actually add all the elements of the array / vector together.

So the executions of the inner loop for the example above would be like this:

Execution 1: 1 + 2 + 3 + 4 + 5
Execution 2: 2 + 3 + 4 + 5
Execution 3: 3 + 4 + 5
Execution 4: 4 + 5
Execution 5: 5

Hope that helps.

thisSum at your code line 8 is reset at beginning part of loop i ,

but thisSum in your loop j is keep adding the array a[ ] element in loop j .


Normally I will substitute value and assume value to understand how the loop work.

Let assume vector a has 3 int elements 10,-20,100

therefore a.size() = 3

//maxSum is initialized in the function
int maxSum = 0;

//Start First i loop
int i = 0; i < 3; 
int thisSum = 0; 

int j = i = 0; j < 3; 
thisSum += a[0];
//thisSum = 10
//10 > 0
if (thisSum > maxSum) maxSum = thisSum = 10;
int j = i = 1; j < 3; 
thisSum += a[1];
//thisSum = -10
// -10 not > 10
int j = i = 2; j < 3; 
thisSum += a[2];
//thisSum = 90
//90 > 10
if (thisSum > maxSum) maxSum = thisSum = 90;
//End First i loop

//Start 2nd i loop
int i = 1; i < 3; 
int thisSum = 0; 

int j = i = 1; j < 3; 
thisSum += a[1];
//thisSum = -20
//-20 not > 90
int j = i = 2; j < 3; 
thisSum += a[2];
//thisSum = 80
//80 not > 90
//End 2nd i loop

//Start 3rd i loop
int i = 2; i < 3; 
int thisSum = 0; 

int j = i = 2; j < 3; 
thisSum += a[2];
//thisSum = 100
//100 > 90
if (thisSum > maxSum) maxSum = thisSum = 100;
//End 3rd i loop

//return 100
//return maxSum

The concept of the function is it try to get the maximum sum by step by step remove the item from smallest index element to the largest index argument.

1st loop i : maxSum = 90

2nd loop i : maxSum = 90 (remove 10)

3rd loop i : maxSum = 100 (remove 10,-20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM