给定一组非负整数组成的数组,代表一组柱状图的高度,其中每个柱子的宽度都是1.在这组柱状图中找到能组成的最大矩形的面积。
#include<iostream>
using namespace std;
int main(int argc, char *argv[]) {
int len = 6;
int height[] = {0,1,0,0,0,0};
int size = GetlargestArea(height, len);
printf("最大面积为%d\n",size);
return 0;
}
int GetlargestArea(int *height,int len){
int big = -1;
int i = 1;
for(i=0;i<len;i++)
{
int width = 1;//拿到宽度
int h = height[i];
for(;width<=len-i;width++)
{
if(h>height[(i+width-1)])
{
--width;
break;
}
}
if(big < h*width)
big = h*width;
}
return big;
}
用的是穷举法