您的位置:首页 > 其它

寻找直方图中面积最大的矩形

2013-07-31 12:09 183 查看
【原创】寻找直方图中面积最大的矩形



// test.cpp : 定义控制台应用程序的入口点。
// editor: Visual C++ 2010 express
// system: win7 x32
// create: 2013-07-31 12:00FM
// author: jiang kejun <jinhua_k9@163.com>

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int largestRectangleArea();
int maxByIndex(int index);
const int map[6] = {2,1,5,6,2,3};
int len = sizeof(map)/sizeof(int);
int _tmain(int argc, _TCHAR* argv[])
{
cout<<largestRectangleArea()<<endl;
system("pause");
return 0;

}

int largestRectangleArea()
{
int max = maxByIndex(0);
for(int i=1;i<len;i++)
{
// maxvalue
if(max<maxByIndex(i)){
max = maxByIndex(i);
}
}
return max;
}
// 算出每个数组元素最大可达多少面积
int maxByIndex(int index)
{
int c=0;
for(int v=0;v<len;v++)
{
// v小于
if(map[index]>map[v])
{
// 我已路过
if(v>index){
break;
}
else{
c=0;
}
}
else{
c++;
}
}
return c*map[index];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: