-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest Rectangle in Histogram.cpp
48 lines (45 loc) · 1.31 KB
/
Largest Rectangle in Histogram.cpp
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
43
44
45
46
47
48
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (height.size() <= 0) {
return 0;
}
vector<int> stack;
vector<int> left;
int max=0;
//stack.push_back(height[0]);
//left.push_back(1);
for (int i=0; i<=height.size(); i++) {
int h;
if (i<height.size()) {
h = height[i];
}
else {
h = 0;
}
if (stack.size()==0 || h>stack.back()) {
stack.push_back(h);
left.push_back(1);
}
else {
int cnt = 1;
while (stack.size()>0 && stack.back()>h) {
int currh = stack.back();
int currl = left.back();
stack.pop_back();
left.pop_back();
int curr = (cnt+currl-1) * currh;
if (curr>max) {
max = curr;
}
cnt+=currl;
}
stack.push_back(h);
left.push_back(cnt);
}
}
return max;
}
};