[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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
辅助栈:方法1(扫描3遍)
辅助栈:方法3
用辅助栈:方法2(扫描一遍)
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 =
10unit.
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; } };
相关文章推荐
- leetcode_question_84 Largest Rectangle in Histogram
- LeetCode: Largest Rectangle in Histogram [084]
- [LeetCode] Largest Rectangle in Histogram
- LeetCode | Largest Rectangle in Histogram
- Leetcode Largest Rectangle in Histogram
- [leetcode]Largest Rectangle in Histogram @ Python
- 【leetcode】Largest Rectangle in Histogram
- largest rectangle in histogram leetcode
- leetcode Largest Rectangle in Histogram 解法二
- Leetcode: Largest Rectangle in Histogram
- LeetCode | Largest Rectangle in Histogram
- leetcode--Largest Rectangle in Histogram
- [leetcode]Largest Rectangle in Histogram
- [Leetcode]Largest Rectangle in Histogram - 非递减栈(递增及相等)
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
- leetcode || 84、Largest Rectangle in Histogram
- leetcode:Largest Rectangle in Histogram
- LeetCode - Largest Rectangle in Histogram
- Largest Rectangle in Histogram -- LeetCode
- LeetCode-Largest Rectangle in Histogram