diff options
| author | Nuno Vieira <nunovieira220@gmail.com> | 2020-11-20 23:09:00 +0000 |
|---|---|---|
| committer | Nuno Vieira <nunovieira220@gmail.com> | 2020-11-20 23:11:09 +0000 |
| commit | c1ebfe6738e02e7185125ba7c76ed263e0ccc753 (patch) | |
| tree | d5bfc370d5f00ebf90e2b0745a74a76b054ecc32 | |
| parent | 0b2547cacbf7b3c10361196b32133e88b9bfd90a (diff) | |
| download | perlweeklychallenge-club-c1ebfe6738e02e7185125ba7c76ed263e0ccc753.tar.gz perlweeklychallenge-club-c1ebfe6738e02e7185125ba7c76ed263e0ccc753.tar.bz2 perlweeklychallenge-club-c1ebfe6738e02e7185125ba7c76ed263e0ccc753.zip | |
Add nunovieira220 perl solution to challenge 087
| -rw-r--r-- | challenge-087/nunovieira220/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-087/nunovieira220/perl/ch-2.pl | 95 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-087/nunovieira220/perl/ch-1.pl b/challenge-087/nunovieira220/perl/ch-1.pl new file mode 100644 index 0000000000..23d1a64951 --- /dev/null +++ b/challenge-087/nunovieira220/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw(say); + +# Input +my @N = (20, 19, 9, 11, 10); + +# Longest Consecutive Sequence +my %nums = (); +my $res = -1; +my $counter = 1; + +for (sort {$a <=> $b} @N) { + if(defined $nums{$_ - 1}) { + my $val = $nums{$_ - 1} + 1; + $nums{$_} = $val; + + if ($val >= $counter) { + $counter = $val; + $res = $_; + } + } else { + $nums{$_} = 1; + $res = $_ if($counter == 1); + } +} + +# Output +say join(',', $res - $counter + 1..$res);
\ No newline at end of file diff --git a/challenge-087/nunovieira220/perl/ch-2.pl b/challenge-087/nunovieira220/perl/ch-2.pl new file mode 100644 index 0000000000..19cae5f6ee --- /dev/null +++ b/challenge-087/nunovieira220/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature qw(say); +use List::Util qw[min]; +use Data::Dumper::OneLine; + +# Input +my @N = ( + [0, 0, 0, 1, 1, 1], + [1, 1, 1, 1, 1, 1], + [0, 0, 1, 0, 0, 1], + [0, 0, 1, 1, 1, 1], + [0, 0, 1, 1, 1, 1], +); + +# Largest Rectangle +my @R = (); +my @points = (); +my $totalArea = 0; + +for (my $x = 0; $x < scalar @N; $x++) { + for (my $y = 0; $y < scalar @{$N[0]}; $y++) { + if($N[$x][$y] == 1) { + my $left = 1, my $up = 1; + my $leftArea = 1, my $upArea = 1, my $maxArea = 1; + my $leftPoint, my $upPoint, my $maxPoint; + + # Count left and up values + $left = @{$R[$x][$y-1]}[0] + 1 if($y > 0); + $up = @{$R[$x-1][$y]}[1] + 1 if($x > 0); + + # Get maximum area on the left side + if($y > 0) { + my $limitUp = $up; + $leftArea = $up; + $leftPoint = [$x - $limitUp + 1, $y]; + + for(my $d = 1; $d < $left; $d++) { + $limitUp = min($limitUp, @{$R[$x][$y-$d]}[1]); + my $area = ($d + 1) * $limitUp; + + if($area > $leftArea) { + $leftArea = $area; + $leftPoint = [$x - $limitUp + 1, $y - $d]; + } + } + } + + # Get maximum area on the upper side + if($x > 0) { + my $limitLeft = $left; + $upArea = $left; + $upPoint = [$x, $y - $limitLeft + 1]; + + for(my $d = 1; $d < $up; $d++) { + $limitLeft = min($limitLeft, @{$R[$x-$d][$y]}[0]); + my $area = ($d + 1) * $limitLeft; + + if($area > $upArea) { + $upArea = $area; + $upPoint = [$x - $d, $y - $limitLeft + 1]; + } + } + } + + # Get maximum area until now + if($leftArea > $upArea) { + $maxPoint = $leftPoint; + $maxArea = $leftArea; + } else { + $maxPoint = $upPoint; + $maxArea = $upArea; + } + + if($maxArea > $totalArea) { + @points = (@$maxPoint[0], @$maxPoint[1], $x, $y); + $totalArea = $maxArea; + } + + $R[$x][$y] = [$left, $up]; + } else { + $R[$x][$y] = [0, 0]; + } + } +} + +# Output +if($totalArea <= 1) { + say "No rectangule was found on the matrix."; +} else { + say "Result rectangule is located between (".$points[0].", ".$points[1].") and (".$points[2].", ".$points[3].") ". + "with total area of ".$totalArea."."; +}
\ No newline at end of file |
