Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Tue, 21 Mar 2006 23:47:42 -0500
"Arthur J. O'Dwyer" wrote:
On Tue, 21 Mar 2006, CBFalconer wrote:
Mark P wrote:
CBFalconer wrote:
Take a look at the solve() function I posted elsethread. Same
basic idea, but I think much simpler. It is much like
maintaining a moving average, where all you need is the history
over the prior window.
Simpler, perhaps, but as far as I can tell, not correct.
Did you plug it into the maxsum driver I published earlier? Please
do so and tell me what you find inaccurate. It is O(N).
% ./a.out 4 2
161531* 51542* 173471* 49805
225013 is the solution
% ./a.out 6 4
161531* 51542* 173471* 49805* 90851 154485*
468612 is the solution
I assume the starred entries are supposed to be the selection? In
that case, since all the entries are positive, and assuming there's
no signed overflow, all the entries should be starred, every time.
Certainly the starred entries shouldn't be non-contiguous, as in
the second example above.
Your choice to use very large positive integers instead of a
mixture of small positive and negative integers may be partly to
blame. Also, it looks like the code for initializing the array
from a random seed is mixed up with the code for computing the
solution, which is icky. I still can't really tell what the code
is supposed to be doing.
The *s mark the point at which any candidate sum is found, i.e. the
last l items form the largest sum found so far. Entries prior to
the start are presumed 0. The last * marks the point at which the
solution sum was found.
The numbers are quite small when run on a machine with 16 bit
integers.
I just used rand to supply a stream of positive integers. Lacking
srand, it will always supply the same sequence on any given
machine. Then I used advance knowledge of the summing to ensure
that no integer overflow could occur.
The array, indexed modulo l, holds the last l entries, and removes
the earliest from the running sum before inserting the current
entry.
I thought it was quite transparent code. Maybe I am wrong in that
assumption.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
.
- Follow-Ups:
- Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- From: Arthur J. O'Dwyer
- Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- References:
- How to find the maximum sum of atleast L consecutive integers given a sequence of N integers.
- From: vb@xxxxxxxxx
- Re: How to find the maximum sum of atleast L consecutive integers given a sequence of N integers.
- From: Mark P
- Re: How to find the maximum sum of atleast L consecutive integersgiven a sequence of N integers.
- From: CBFalconer
- Re: How to find the maximum sum of atleast L consecutive integersgiven a sequence of N integers.
- From: Mark P
- Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- From: CBFalconer
- Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- From: Arthur J. O'Dwyer
- How to find the maximum sum of atleast L consecutive integers given a sequence of N integers.
- Prev by Date: Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- Next by Date: I have silly question...
- Previous by thread: Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- Next by thread: Re: How to find the maximum sum of atleast L consecutive integersgivena sequence of N integers.
- Index(es):
Relevant Pages
|
Loading