Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??
And here is an optimized version. Add in the question of what optimizations are present, and why they work. Some are more obvious than others. The thoughtful hacker might also wonder at why the \$max_mid, \$min_mid logic switched.

Oh right. And this adds the restriction that the heights should be integers. I think that is a reasonable rule for the speedup...

```  use strict;
{
use integer;
my @sizes;
my %ans;
my @align;
sub group_cats {
(my \$num, @sizes) = @_;
%ans = ();
my \$a = 1;
for my \$i (1..\$#sizes) {
if (\$a + \$a < \$i) {
\$a += \$a;
}
\$align[\$i] = \$a;
}
\$align[0] = 0;
my (\$size, @partition) = _group_cats(\$num, 0, \$#sizes);
return @partition;
}

sub _group_cats {
my \$key = join ":", @_;
my (\$num, \$start, \$end) = @_;
if (not exists \$ans{\$key}) {
if (\$num < 1) {
\$ans{\$key} = [0];
}
elsif (1 == \$num) {
my @part = @sizes[\$start..\$end];
my \$sum = 0;
\$sum += \$_ for @part;
\$ans{\$key} = [\$sum, \@part];
}
else {
my \$num_a = \$align[\$num];
my \$num_b = \$num - \$num_a;

my \$min_mid = \$start + \$num_a - 1;
my \$max_mid = \$end - \$num_b;

my \$mid = \$min_mid + \$align[\$max_mid - \$min_mid];

my (\$last_a, @part_a) = _group_cats(\$num_a, \$start, \$mid);
my (\$last_b, @part_b) = _group_cats(\$num_b, \$mid + 1, \$end);
my \$best = \$last_a < \$last_b ? \$last_b : \$last_a;
my @best_part = (@part_a, @part_b);
while (\$min_mid < \$max_mid) {
if (\$last_b <= \$last_a) {
if (\$max_mid > \$mid) {
\$max_mid = \$mid;
}
else {
\$max_mid--;
}
}
else {
\$min_mid = \$mid;
}
\$mid = \$min_mid + \$align[\$max_mid - \$min_mid];
(\$last_a, @part_a) = _group_cats(\$num_a, \$start, \$mid);
(\$last_b, @part_b) = _group_cats(\$num_b, \$mid + 1, \$end);
my \$max = \$last_a < \$last_b ? \$last_b : \$last_a;
if (\$max < \$best) {
\$best = \$max;
@best_part = (@part_a, @part_b);
}
}
\$ans{\$key} = [\$best, @best_part];
}
}
return @{\$ans{\$key}};
}
}
BTW the original question had an straightforward recursive solution that scaled exponentially. I found that too easy, hence the faster solution.

In reply to Re: Puzzle for the puzzle: Re: Puzzle... by Anonymous Monk
in thread Puzzle: need a more general algorithm by Ovid

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [SuicideJunkie]: LanX So, basically, you're saying that Trump and Trump were the only ones doing it wrong there.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2018-03-22 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (286 votes). Check out past polls.

Notices?