diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-30 23:08:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-30 23:08:38 +0100 |
| commit | 734c740fc0fa460b7ce20e482ee9ffad1b93dc6a (patch) | |
| tree | 670cc92de45ed8713b5a995a3549e7b52f266829 | |
| parent | c44e1fabcd3e8d21fb9d5e38ce36e48d66bdb154 (diff) | |
| parent | d61e52113cd9222ba8c876002997ba4bc1a0367c (diff) | |
| download | perlweeklychallenge-club-734c740fc0fa460b7ce20e482ee9ffad1b93dc6a.tar.gz perlweeklychallenge-club-734c740fc0fa460b7ce20e482ee9ffad1b93dc6a.tar.bz2 perlweeklychallenge-club-734c740fc0fa460b7ce20e482ee9ffad1b93dc6a.zip | |
Merge pull request #2178 from WmMonty/challenge-075
Happy that I got this one done. Well, I couldn't quite finish the fir…
| -rwxr-xr-x | challenge-075/will-west/perl/75.2.pl | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/challenge-075/will-west/perl/75.2.pl b/challenge-075/will-west/perl/75.2.pl new file mode 100755 index 0000000000..460415aff1 --- /dev/null +++ b/challenge-075/will-west/perl/75.2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl +use 5.28.1; +use warnings; +use List::Util 'max'; +use Data::Dumper; + + + +#TASK #2 › Largest Rectangle Histogram +#Submitted by: Mohammad S Anwar +# +#You are given an array of positive numbers @A. +# +#Write a script to find the largest rectangle histogram created by the given array. + + +#my solution takes advantage of the fact that a chart like what we're working with never has vertical gaps. So, we just need to find contiguous horizontal lines and square them with the distance from the chart's base. + +#I promise to try to do proper testing for the next one! + + +my @chart = getchart(); +my @rows = makerowsfromchart(@chart); +my @contiguous = contiguousinrows(@rows); +my @squares = square(@contiguous); +my $biggestsquare = max(@squares); +say $biggestsquare; + + + + + +sub getchart{ + (1,4,6,4,2,2,4,5,5) +} + + + + + + + +sub makerowsfromchart{ + my $mx = max @_; + my @rows; + for my $row (1..$mx){ + for my $col (@_){ + push @{$rows[$row]}, int($col>=$row) + } + } + @rows +} + +sub contiguousinrows{ + my @longests; + for my $row (1..$#_){ + my $str = join '',@{$_[$row]}; + my @contigs = ($str=~/(1+)/g); + my @lengths =map{length($_)}@contigs; + push @longests, [$row, max(@lengths)]; + } + @longests +} + + +sub square{ + map{ $_->[0] * $_->[1] } @_ +} + + + + + +sub DEPRECATEDsquare{ + my @sqrd; + push @sqrd, $_->[0] * $_->[1] for @_; + @sqrd +} |
