in reply to Challenge: Sorting Sums Of Sorted Series
Hi Monks
The following algorithm seems to need 4+N+M memory ... and therefore fulfills the challenge :-)
So in additon to N and M, you need the constant space for 4 variables (i, j, val, nextval) and for the temporary sums. :-)
However the runtime would be O(N^2*M^2) (*) - so I wouldn't consider it efficient ;-)
Rata
(*) 2 * N * M for the inner loop - and a maximum N * M for the outer one
note: the restriction that N and M are sorted can easily be dropped with the algorithm above (just replace line 1 with lines 2.2.-2.5)
update: Limbic~Region just told me that blokhead posted nearly the same solution before (he needs one variable more, which doesn't affect the complexity O((NM)^2). ... I didn't want to waste bandwidth, but I fear duplicated solutions are the risk if one is not cheating ;-)
|
|---|