Can you please explain what the code does?
public class Solution3 {
/**
* Linear maximum contiguous subsequence sum
algorithm.
* seqStart and seqEnd represent the actual best
sequence.
*/
public static int maximumSubsequenceSum( int [
] a )
{
int maxSum = 0;
int thisSum = 0;
for( int i = 0, j = 0; j < a.length; j++
)
{
thisSum += a[ j ];
if( thisSum > maxSum )
{
maxSum = thisSum;
int seqStart = i;
int seqEnd = j;
}
else if( thisSum < 0 )
{
i = j + 1;
thisSum = 0;
}
}
return maxSum;
}
public static int[] theMCSS(int [] A) {
int maxSum
= 0;
int
seqStart = -1;
int seqEnd
= -1;
for( int i
= 0; i < A.length; i++ )
for( int j
= i; j < A.length; j++ ){
int
thisSum = 0;
for(
int k = i; k <= j; k++ )
thisSum +=
A[ k ];
if(
thisSum > maxSum ){
maxSum
= thisSum;
seqStart
= i;
seqEnd =
j;
}
}
int[]
r= {seqStart,seqEnd};
return
r;
}
}
Can you please explain what the code does? public class Solution3 { /** * Linear maximum contiguous subsequence
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am