in reply to Longest Increasing Subset
There are two answers to your input.
#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1174320 use strict; use warnings; my @answers; my @queue = [[], [qw(2 6 5 7 4 3 9)]]; while( my $state = shift @queue ) { my @have = $state->[0]->@*; my @more = $state->[1]->@*; if( @more ) { @have == 0 || $have[-1] < $more[0] and push @queue, [[@have, $more[0]], [@more[1..$#more]]]; push @queue, [[@have], [@more[1..$#more]]]; } elsif( @have >= $#answers ) { push @{$answers[@have]}, "@have"; } } print for $answers[-1]->@*;
output is:
2 6 7 9 2 5 7 9
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Longest Increasing Subset
by gilthoniel (Novice) on Oct 20, 2016 at 15:55 UTC | |
by tybalt89 (Monsignor) on Oct 20, 2016 at 18:03 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2016 at 19:58 UTC | |
by Dallaylaen (Chaplain) on Oct 24, 2016 at 08:49 UTC | |
by BrowserUk (Patriarch) on Oct 24, 2016 at 09:30 UTC | |
| |
by pryrt (Abbot) on Oct 20, 2016 at 18:35 UTC |