Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^2: Largest Sum of Consecutive Integersby pKai (Priest) |
on Aug 31, 2006 at 10:15 UTC ( [id://570573]=note: print w/replies, xml ) | Need Help?? |
As for the interesting case of all negative numbers noted by BrowserUK++, coming from the initial "best" intervall being an empty list with sum zero, which will then serve as an upper limit not to surpass in the loop. I think that can be dealt with by Update: hiding original approach in readmore tags, since it is incorrect and now obsolete
As in:
(start Update insert)
So instead of messing that much with tilly's code I now concentrate on the "best" initialization before we enter the loop.
For my addendum loops through the beginning of @array as long as we are still seeing negative numbers.
When we finally see a non-negative we proceed with tilly's code from there.
To be prepared for the case that all elements are negative, we adjust the *_start, *_end, best*, cur* variables heading for the max (= least negative number) instead.
Which leaves me with some code not so elegant anymore
(end update insert) Otherwise I think the idea behind the algorithm presented by tilly++ is correct, or so I hope.
In Section
Seekers of Perl Wisdom
|
|