forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestRectangleArea.cpp
More file actions
42 lines (39 loc) · 1.05 KB
/
largestRectangleArea.cpp
File metadata and controls
42 lines (39 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
height.push_back(0);
int n = height.size();
int ret = 0, i = 0;
vector<int> que;
while (i < n) {
if (que.empty() || height[que.back()] <= height[i]) {
que.push_back(i++);
} else {
while (!que.empty() && height[que.back()] >= height[i]) {
int tp = que.back();
que.pop_back();
int wid = que.empty() ? i : i - 1 - que.back();
ret = max(ret, height[tp] * wid);
}
}
}
return ret;
}
};
int main() {
Solution s = Solution();
vector<int> mm;
mm.push_back(2);
mm.push_back(1);
mm.push_back(5);
mm.push_back(6);
mm.push_back(2);
mm.push_back(3);
cout << s.largestRectangleArea(mm) << endl;
return 0;
}