直方圖最大矩形面積
阿新 • • 發佈:2019-02-12
給定一組非負整陣列成的陣列,代表一組柱狀圖的高度,其中每個柱子的寬度都是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; }
用的是窮舉法