首页 > 其他 > 详细

LeetCode|Largest Rectangle in Histogram

时间:2015-03-26 17:57:01      阅读:294      评论:0      收藏:0      [点我收藏+]

题目:

Given n non-negative integers representing the histogram‘s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

技术分享

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

技术分享

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given height = [2,1,5,6,2,3],
return 10.

思路:

暂时只想到了一个最容易想到的思路,对于每一个直方图,要么他自己是目标矩阵,要么和周围的直方图合并组成目标矩阵,那么对于每一个直方图,试图找到所有的合适的方法即可。

#include <iostream>
#include <vector>
#include <stack>
#include <limits>

using namespace std;
 
int Longestest_rectangle(vector<int>& vec)
{
	int i,j;
	int max=0;
	int hight;
	int tmp;
	for(i=0;i<vec.size();i++)
	{
		hight = numeric_limits<int>::max();
		for(j=i;j<vec.size();j++)
		{
			if(hight > vec[j])
				hight = vec[j];
			max = max>(j-i+1)*hight?max:(j-i+1)*hight;
		}
	}
	return max;
} 




int main()
{
	int array[]={2,1,5,6,2,3};
	vector<int> vec(array,array+sizeof(array)/sizeof(int));
	cout<<Longestest_rectangle(vec)<<endl;
	return 0;
}


LeetCode|Largest Rectangle in Histogram

原文:http://blog.csdn.net/yusiguyuan/article/details/44649993

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!