LeetCode之Largest Rectangle in Histogram

转载 2015年07月09日 22:48:12
/*用栈维持一个不递减的数组的下标。用以获取某个连续段的最大矩形。
参考自:https://github.com/soulmachine/leetcode*/
class Solution {
public:
	int largestRectangleArea(vector<int>& height) {
		if(height.empty()) return 0;
		height.push_back(0);
		stack<int> s;
		int res(0), i(0);
		while(i < height.size()){
			if(s.empty() || height[i] >= height[s.top()]) s.push(i);//入栈
			else{//出栈
				int h = height[s.top()];
				while(!s.empty() && height[i] < height[s.top()]){//出栈
					if(height[s.top()] < h) h = height[s.top()];
					int tmp = s.top();
					s.pop();
					res = max(res, (s.empty() ? i : i - s.top()-1)*h);
				}
				s.push(i);
			}
			++i;
		}
		return res;
	}
};

LeetCode Largest Rectangle in Histogram

  • 2015年06月22日 23:25
  • 2.67MB
  • 下载

[leetcode] 84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar i...

leetcode No84. Largest Rectangle in Histogram

Question: Given n non-negative integers representing the histogram's bar height where the width o...

[C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)

题目: Given n non-negative integers representing the histogram's bar height where the width of each b...

LeetCode-84.Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is...

Largest Rectangle in Histogram -- LeetCode

原题链接: http://oj.leetcode.com/problems/largest-rectangle-in-histogram/  这道题brute force的方法很直接,就是对于每一个窗...

leetcode84: Largest Rectangle in Histogram

84. Largest Rectangle in Histogram height的内容是 [5,6,7,8,3],特点是除了最后一个,前面全部保持递增,且最后一个立柱的高度小于前面所有立柱高度。对...

Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】

题目: Given n non-negative integers representing the histogram's bar height where the width of each ...

[LeetCode] 84. Largest Rectangle in Histogram

[LeetCode] 84. Largest Rectangle in HistogramGiven n non-negative integers representing the histogra...

leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

Largest Rectangle问题题目描述Given n non-negative integers representing the histogram’s bar height where t...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode之Largest Rectangle in Histogram
举报原因:
原因补充:

(最多只允许输入30个字)