-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRect.cpp
43 lines (40 loc) · 897 Bytes
/
LargestRect.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
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
if (height.size()==0) return 0;
stack<int> h;
stack<int> w;
int ans = 0;
for (int i = 0; i<height.size();i++){
while (h.size()!=0 && h.top()>=height[i]) {
int th,tw;
th = h.top();
tw = w.top();
h.pop();
w.pop();
int lw;
if (h.empty()) lw = -1;
else lw = w.top();
if (th == height[i]) tw++;
if (th*(i-1-lw)>ans)
ans = th*(i-1-lw);
}
h.push(height[i]);
w.push(i);
}
int lw = height.size();
while (!h.empty()) {
int th,tw;
th = h.top();
tw = w.top();
h.pop();
w.pop();
int sw;
if (h.empty()) sw = 0;
else sw = w.top()+1;
if (th*(lw-sw)>ans)
ans = th*(lw-sw);
}
return ans;
}
};