leetcode_84_Largest Rectangle in Histogram

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

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;
    }
};
时间: 2024-08-28 16:14:29

leetcode_84_Largest Rectangle in Histogram的相关文章

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. 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

(数组)Largest Rectangle in Histogram(栈解问题)

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. Above is a histogram where width of each bar is 1, given heigh

LeetCode: Largest Rectangle in Histogram [084]

[题目] 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 lar

leetcode第一刷_Largest Rectangle in Histogram

很难的问题,数组线性时间. 属于我之前说的解法的借助辅助空间.给定两个柱子,他们之间的面积由什么确定呢?没错,他们之间的距离和他们之间最矮的那个柱子的高度.我们并不知道这个柱子在什么位置,所以只能遍历,这个复杂度就上去了.那有没有一个方法避免找中间小柱子呢?先想什么情况下不用找.除非你知道枚举出两个位置之间的柱子都比现在这个长.所以想到了用一个栈来保存较长的那些柱子编号.需要保证的一件事是栈里面的柱子高度一定是递增的,每次计算面积的时候是从当前位置往前计算的,计算一次弹出一次,每次都使用栈顶的高

84. Largest Rectangle in Histogram *HARD* 柱状图求最大面积 85. Maximal Rectangle *HARD*

1. 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 large

Leetcode:Largest Rectangle in Histogram 最大矩形面积

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. Above is a histogram where width of each bar is 1, given heigh

C++]LeetCode: 133 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. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The larg

LeetCode &quot;Largest Rectangle in Histogram&quot; - TRICKY MONO-QUEUE

I got a DP solution first which is O(n^2). Apparently it is not a optimized one - think about: it is linear value space. There must be O(n) solution. And yes there is: http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html

【leetcode刷题笔记】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. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest