in reply to Re^4: Brute force vs algorithm (PWC # 100)
in thread Brute force vs algorithm (PWC # 100)
For people who don't like recursion and hashes :)
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11128406 use warnings; use List::Util qw( reduce ); local $_ = <<END; 1 2 4 6 4 9 5 1 7 2 END my @d = map [ split ], split /\n/; for my $r ( reverse 0 .. $#d - 1 ) { for my $c ( 0 .. $r ) { $d[$r][$c] .= ' + ' . reduce { eval $a <= eval $b ? $a : $b } $d[$r+1][$c], $d[$r+1][$c+1]; } } print "$d[0][0]\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Brute force vs algorithm (PWC # 100)
by tybalt89 (Monsignor) on Feb 16, 2021 at 05:04 UTC |