Dear monks,
A holiday ponderance.

Given a hash consisting of key integers > value integers, remove the fewest number of key/value pairs such that all remaining pairs are "nested" (loosely used!! see examples). Integers may only appear once in the hash. Note that there are equally optimal solutions in some (many?) cases, and not all need be returned. A solution which loops through left to right and removes non-nested pairs as they are encountered (using some look-ahead to detect this) is simple, but AFAIKT not guaranteed to remove the fewest number of pairs? I may be wrong on this, in which case this question is pointless. Of course this is not Perl specific- but Perl is what I'm using, and perhaps there some nice hash-foo which will work!

Some trivial examples with letters, to show possible input patterns:

1. a b B c A C <- bad 2. a b B A c C <- ok 3. a b c C B A <- ok 4. a b A B c C <- bad 5. a b B c C A d e E D <- ok 6. a A b B c C <- ok
'solved'
1. a b B A (removed cC) or- b B c C (removed aA)
4. remove aA or bB

Some play data.

%k = ( 0 => 8, 3 => 5, 7 => 9, 10 => 100, 11 => 99, 15 => 25, 16 => 26, 17 => 27, 35 => 40, 36 => 39, 37 => 38, 60 => 80, 65 => 75, 66 => 76, 67 => 74);

Happy qw/Holidays Christmas Festivus Kwanzaa yet-another-day-of-meditation-with-no-otherwise-special-meaning/

P.S. Over-caffeinated monks- this is honestly not homework, or a final exam question, or even class related, so don't bother pointing out the fact its sounds like such. :) Thank you.


In reply to (non) nested pairs by xipho

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.