The topic is to calculate the maximum rectangular area of the histogram, the following is my practice, I also see on the internet that some people say can be solved by the way of the stack, because of the time problem, and do not try immediately, next time in a try it!!
And there's the variable: the largest rectangular area of the matrix, where the elements in the matrix can only be 1 and 0, the code will be reissued next time!!
The code is as follows:
#include <iostream>
using namespace Std;
int maxsquare (const int POS,CONST int N,const int height[])
{
if (n==1)
return height[0];
int square=0;
for (int i=pos;i<n;i++)
{
int count=1,temp_square=0;
int j=i-1;
while (j>=0)
{
if (Height[j--]>=height[i])
count++;
Else
Break
}
j=i+1;
while (J<n)
{
if (Height[j++]>=height[i])
count++;
Else
Break
}
Temp_square=count*height[i];
if (Temp_square>square)
Square=temp_square;
}
return square;
}
int main ()
{
int square,n,temp;
int height[100000];
cin>>n;
for (int i=0;i<n;i++)
{
cin>>temp;
Height[i]=temp;
}
Square=maxsquare (0,n,height);
cout<<square<<endl;
return 0;
}
Calculate the maximum rectangular area of a histogram