首页 > 代码库 > leetcode-Largest Rectangle in Histogram

leetcode-Largest Rectangle in Histogram

 

 1 #include <iostream> 2 #include <stack> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6  7 class Solution { 8 public: 9     int largestRectangleArea(vector<int> &height) {10         stack<int> s;11         height.push_back(0);12         int result = 0;13         for (int i = 0; i < height.size();) {14             if (s.empty() || height[i] > height[s.top()])15                 s.push(i++);16             else {17                 int tmp = s.top();18                 s.pop();19                 result = max(result, height[tmp] * (s.empty() ? i : i - s.top() - 1));20             }21         }22         return result;23     }24 };25 int main()26 {27     Solution s;28     int a[] = { 2, 3, 4, 5, 6 };29     vector<int> v(a, a + 5);30     cout << s.largestRectangleArea(v) << endl;31     return 0;32 }

 

leetcode-Largest Rectangle in Histogram