码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-Largest Rectangle in Histogram

时间:2014-10-12 23:59:48      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

 

 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

标签:style   blog   color   io   os   ar   for   sp   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!