码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_84_Largest Rectangle in Histogram

时间:2015-02-10 11:25:41      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   stack   

麻烦各位朋友帮忙顶一下增加人气,如有错误或疑问请留言纠正,谢谢技术分享


Largest Rectangle in Histogram

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.

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


//vs2012测试代码
//此解法的核心思想为:一次性计算连续递增的区间的最大面积,并且考虑完成这个区间之后,考虑其前、后区间的时候,不会受到任何影响。也就是这个连续递增区间的最小高度大于等于其前、后区间。

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

using namespace std;

#define N 6

class Solution {
public:
    int max(int a, int b){return a > b ? a : b;}
    int largestRectangleArea(vector<int> &height) {
    	height.push_back(0);
        stack<int> temp;
        int i = 0;
        int maxArea = 0;
        while(i < height.size())
		{
            if(temp.empty() || height[temp.top()] <= height[i])
			{
                temp.push(i);
				i++;
			}
            else 
			{
                int t = temp.top();
				temp.pop();
                maxArea = max(maxArea, height[t] * (temp.empty() ? i : i - temp.top() - 1));
            }
        }
        return maxArea;
    }
};

int main()
{
	int a;
	vector<int> height;
	for(int i=0; i<N; i++)
	{
		cin>>a;
		height.push_back(a);
	}
	Solution lin;
	cout<<lin.largestRectangleArea(height)<<endl;
}

//方法一:自测Accepted
//此解法的核心思想为:一次性计算连续递增的区间的最大面积,并且考虑完成这个区间之后,考虑其前、后区间的时候,不会受到任何影响。也就是这个连续递增区间的最小高度大于等于其前、后区间。

class Solution {
public:
    int max(int a, int b){return a > b ? a : b;}
    int largestRectangleArea(vector<int> &height) {
    	height.push_back(0);
        stack<int> temp;
        int i = 0;
        int maxArea = 0;
        while(i < height.size())
		{
            if(temp.empty() || height[temp.top()] <= height[i])
			{
                temp.push(i);
				i++;
			}
            else 
			{
                int t = temp.top();
				temp.pop();
                maxArea = max(maxArea, height[t] * (temp.empty() ? i : i - temp.top() - 1));
            }
        }
        return maxArea;
    }
};

//方法:超时
int largestRectangleArea(vector<int> &height) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int end = height.size();
    	int begin = 0;
		int largestarea = 0;
		for(int i = begin; i < end; ++i)
		{
			int area;
			int high = height[i];
			for(int j = i; j < end; ++j){
				if(height[j] < high) high = height[j];
				area = (j - i + 1)*high;
				if(area > largestarea) largestarea = area;
			}
		}
		return largestarea;
    }

//方法二:其他版本
//再仔细想想哪些地方做了重复的工作。
//以 2 1 3 4 5为例,
//我们已经知道R[2] = 4 (height[2] = 3),对于R[1] (height[1] = 1),由于height[1] <= height[2],因此没必要再遍历2到R[2]这一段,即3 4 5,。
//因为height[i] <= height[i + 1] <= ... <= height[R[i]],
//因此如果height[i - 1] <= height[i],则R[i - 1] >= R[i],所以我们可以直接比较height[i - 1]与height[R[i] + 1],直到找到height[i - 1] > height[R[j] + 1]。

class Solution {
public:
    int largestRectangleArea(vector<int> &height) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int n = height.size();
        int l[n], r[n];
        memset(l, 0, sizeof(int) * n);
        memset(r, 0, sizeof(int) * n);
        l[0] = 0;
        for (int i = 1; i < n; i++) {
            if (height[i] > height[i - 1]) {
                l[i] = i;
            }
            else {
                int idx = l[i - 1];
                while (idx > 0 && height[i] <= height[idx - 1]) {
                    idx = l[idx - 1];
                }
                l[i] = idx;
            }
        }
        r[n - 1] = n - 1;
        for (int i = n - 2; i >= 0; i--) {
            if (height[i] > height[i + 1]) {
                r[i] = i;
            }
            else {
                int idx = r[i + 1];
                while (idx < n - 1 && height[i] <= height[idx + 1]) {
                    idx = r[idx + 1];
                }
                r[i] = idx;
            }
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            if ((r[i] - l[i] + 1) * height[i] > max) {
                max = (r[i] - l[i] + 1) * height[i];
            }
        }
        return max;
    }
};


leetcode_84_Largest Rectangle in Histogram

标签:c++   leetcode   stack   

原文地址:http://blog.csdn.net/keyyuanxin/article/details/43699405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!