Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
All,
Yesterday, I grumbled in the CB that there had been a lack of challenging problems that were interesting lately. ikegami was kind enough to suggest sorting the sums of two sorted lists. For instance:
1 2 3 4 5 6 7 8 3, 5, 5, 7, 7, 7, 9, 9, 9, 9, 11, 11, 11, 13, 13, 15

Now obviously, there is nothing challenging about this. Perform a nested loop to collect all sums and then sort the remaining list. The challenge then is that memory consumption needs to be less than N + M + N*M where N and M represent the number of elements in the respective lists. Of course you could cheat by putting things out on disk or what not, but that really isn't in the spirit of the challenge. So - what is the least amount of memory you can use above N + M? Take it as a given that you can store both lists in memory and that you will always know which of the two lists has fewer elements if not the same.

I have 3 solutions where I have whittled the memory consumption down. The best only requires N memory above N + M where N represents the smaller of the two lists. I haven't Benchmarked it against the others but there are virtual bonus points for being both memory efficient and fast.

Cheers - L~R


In reply to Challenge: Sorting Sums Of Sorted Series by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-24 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found