RosettaCodeData/Task/Longest-increasing-subsequence/Perl-6/longest-increasing-subsequence-2.pl6

17 lines
450 B
Raku
Raw Permalink Normal View History

2015-02-20 09:02:09 -05:00
sub lis(@deck is copy) {
2015-11-18 06:14:39 +00:00
my @S = [@deck.shift() => Nil].item;
2015-02-20 09:02:09 -05:00
for @deck -> $card {
2015-11-18 06:14:39 +00:00
with first { @S[$_][*-1].key > $card }, ^@S -> $i {
@S[$i].push: $card => @S[$i-1][*-1] // Nil
2015-02-20 09:02:09 -05:00
} else {
2015-11-18 06:14:39 +00:00
@S.push: [ $card => @S[*-1][*-1] // Nil ].item
2015-02-20 09:02:09 -05:00
}
}
reverse map *.key, (
@S[*-1][*-1], *.value ...^ !*.defined
)
}
say lis <3 2 6 4 5 1>;
say lis <0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15>;