Can you please explain what the code
does?
public class Solution2 {
/**
* Quadratic maximum contiguous subsequence sum
algorithm.
* seqStart and seqEnd represent the actual best
sequence.
*/
public static int maxSubsequenceSum( int [ ] a
)
{
int maxSum = 0;
for( int i = 0; i < a.length; i++ )
{
int thisSum = 0;
for( int j = i; j < a.length; j++ )
{
thisSum += a[ j ];
if( thisSum > maxSum )
{
maxSum = thisSum;
int seqStart = i;
int seqEnd = j;
}
}
}
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 Solution2 { /** * Quadratic maximum contiguous subseq
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am