1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# LargestRectangleHistogram.pm
#=============================================================================
# Copyright (c) 2020, Bob Lied
#=============================================================================
# Description:
#=============================================================================
package LargestRectangleHistogram;
use strict;
use warnings;
use feature qw(say);
use List::Util qw(max min);
use List::MoreUtils qw(arrayify); # Flattens 2D array into a list
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw();
sub _setup
{
my $self = shift;
for my $row ( 0 .. $self->{_numRow} -1 )
{
for my $col ( 0 .. $self->{_numCol}-1 )
{
$self->{_grid}->[$row][$col] = ( $self->{_list}->[$col] > $row ? '*' : ' ' );
$self->{_area}->[$row][$col] = 0;
}
}
}
sub new
{
my $class = shift;
$class = ref($class) || $class;
my $self = {
_list => [ @_ ],
_numCol => scalar(@_),
_numRow => List::Util::max( @_ ),
_grid => undef,
_area => undef,
_extent => undef,
};
bless $self, $class;
$self->_setup();
return $self;
}
sub _extendLeft
{
my $self = shift;
my ($r, $col) = @_;
my $row = $self->{_grid}->[$r];
$col-- while ( $col >= 0 && $row->[$col] eq '*' );
return $col+1;
}
sub _extendRight
{
my $self = shift;
my ($r, $col) = @_;
my $row = $self->{_grid}->[$r];
$col++ while ( $col < $self->{_numCol} && $row->[$col] eq '*' );
return $col -1;
}
sub _extend
{
my $self = shift;
my ($row, $col) = @_;
my $ext = $self->{_extent};
# Memoize the range if already calculated
return @{$ext->[$row][$col]} if exists $ext->[$row][$col];
my $maxCol = $self->_extendRight($row, $col);
my $minCol = $self->_extendLeft($row, $col);
$ext->[$row][$col] = [ $minCol, $maxCol ] for $minCol .. $maxCol;
return ($minCol, $maxCol);
}
sub _findArea
{
my $self = shift;
my ($row, $col) = @_;
my $grid = $self->{_grid};
my ($minCol, $maxCol) = $self->_extend($row, $col);
my $height = List::Util::min( @{$self->{_list}}[$minCol..$maxCol] );
my $a = $self->{_area}->[$row][$col] = ( ( $maxCol - $minCol + 1) * $height );
# say "[$row][$col] : maxL=$minCol maxR=$maxCol height=$height area=$a";
return $a;
}
sub findLRH
{
my $self = shift;
my $grid = $self->{_grid};
for my $row ( 0 .. $self->{_numRow}-1 )
{
for my $col ( 0 .. $self->{_numCol}-1 )
{
next unless $grid->[$row][$col] eq '*';
my $area = $self->_findArea($row, $col);
}
}
return max(arrayify( @{$self->{_area}} ) );
}
sub _show
{
my $self = shift;
my $g = $self->{_grid};
my $numRow = $self->{_numRow}-1;
my $numCol = $self->{_numCol}-1;
print " ";
for my $c ( 0 .. $numCol )
{
print "[$c]";
}
print "\n";
for my $r ( 0 .. $numRow )
{
print "[$r] ";
say " ", join(' ', @{$g->[$r]}), " ";
}
}
sub display
{
my $self = shift;
my @chart;
for ( my $row = $self->{_numRow}-1 ; $row >= 0 ; $row-- )
{
printf("%2d| ", $row+1);
my $line = join(' ', @{$self->{_grid}->[$row]});
say $line;
}
say ' +', ('-' x ($self->{_numCol}*2));
say ' ', join(' ', @{$self->{_list}});
}
1;
|