![]() |
|
Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Challenge: Sorting Sums Of Sorted Seriesby blokhead (Monsignor) |
on Feb 02, 2010 at 22:46 UTC ( #821053=note: print w/replies, xml ) | Need Help?? |
You can always save space by never storing anything -- just recompute a sum every time you need it.. Here's a solution that uses constant space:
At each iteration, it traverses both lists to find the next smallest possible sum, and how many times it occurs. It only needs to keep track of $min, $nextmin, $multiplicity, $i, $j. Of course the tradeoff is the running time, which is O((NM)2). Perhaps the metric should be to minimize the product of time space complexities? For comparison, naively computing all sums and sorting uses NM space and NM log(NM) time, so it's slightly worse than mine under the combined metric. Update: extra parens around print statement.. blokhead
In Section
Seekers of Perl Wisdom
|
|