首页 > 其他 > 详细

leetcode-Largest Rectangle in Histogram

时间:2014-10-12 22:50:59      阅读:345      评论:0      收藏:0      [点我收藏+]

 

 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

原文:http://www.cnblogs.com/forcheryl/p/4021208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!