Calculate the maximum rectangular area of a histogram

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.