Fix for max subarray sum
This commit is contained in:
parent
f269ae3919
commit
2755a634d8
|
@ -487,8 +487,8 @@ for each ending position from left to right.
|
||||||
|
|
||||||
The following code implements the algorithm:
|
The following code implements the algorithm:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
int best = 0, sum = 0;
|
int best = array[0], sum = array[0];
|
||||||
for (int k = 0; k < n; k++) {
|
for (int k = 1; k < n; k++) {
|
||||||
sum = max(array[k],sum+array[k]);
|
sum = max(array[k],sum+array[k]);
|
||||||
best = max(best,sum);
|
best = max(best,sum);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue