您的位置:首页 > 其它

[LeetCode] Largest Rectangle in Histogram

2012-11-25 11:30 399 查看
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
.

辅助栈:方法1(扫描3遍)

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> area(height.size());

stack<int> s;
for(int i = 0; i < height.size(); i++)
if (s.empty())
{
s.push(i);
area[i] = 0;
}
else
{
while(!s.empty())
{
if (height[i] <= height[s.top()])
{
s.pop();
}
else
break;
}

if (s.empty())
area[i] = i;
else
area[i] = i - s.top() - 1;
s.push(i);
}

while(!s.empty())
s.pop();

for(int i = height.size() - 1; i >= 0; i--)
if (s.empty())
{
s.push(i);
area[i] += 0;
}
else
{
while(!s.empty())
{
if (height[i] <= height[s.top()])
{
s.pop();
}
else
break;
}

if (s.empty())
area[i] += height.size() - i - 1;
else
area[i] += s.top() - i - 1;
s.push(i);
}

int maxArea = 0;
for(int i = 0; i < area.size(); i++)
{
maxArea = max(maxArea, height[i] * (area[i] + 1));
}

return maxArea;
}
};


辅助栈:方法3

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxArea = 0;
stack<int> s;

for(int i = 0; i < height.size(); i++)
if (s.empty())
{
s.push(i);
}
else
{
while(!s.empty())
{
if (height[s.top()] <= height[i])
{
s.push(i);
break;
}
else
{
int index = s.top();
s.pop();
int leftWidth = s.empty() ? index : index - s.top() - 1;
int rightWidth = i - index - 1;
maxArea = max(maxArea, height[index] * (leftWidth + rightWidth + 1));
}
}

if (s.empty())
s.push(i);
}

int rightIndex = s.empty() ? 0 : s.top() + 1;

while(!s.empty())
{
int index = s.top();
s.pop();
int leftWidth = s.empty() ? index : index - s.top() - 1;
int rightWidth = rightIndex - index - 1;
maxArea = max(maxArea, height[index] * (leftWidth + rightWidth + 1));
}

return maxArea;
}
};


用辅助栈:方法2(扫描一遍)

struct Node
{
int val;
int index;
int area;
Node(){}
Node(int v, int idx):val(v), index(idx){}
};

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<Node> s;
int maxArea = 0;
for(int i = 0; i < height.size(); i++)
if (s.empty())
s.push(Node(height[i], i));
else
{
while(!s.empty())
{
Node node = s.top();
if (node.val <= height[i])
{
s.push(Node(height[i], i));
break;
}
else
{
s.pop();
int leftIndex = s.empty() ? 0 : s.top().index + 1;
maxArea = max(maxArea, (i - node.index + node.index - leftIndex) * node.val);
}
}

if (s.empty())
s.push(Node(height[i], i));
}

int index = height.size();
while(!s.empty())
{
Node node = s.top();
s.pop();
int leftIndex = s.empty() ? 0 : s.top().index + 1;
maxArea = max(maxArea, (index - node.index + node.index - leftIndex) * node.val);
}

return maxArea;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: