-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path84.cpp
More file actions
54 lines (52 loc) · 1.36 KB
/
84.cpp
File metadata and controls
54 lines (52 loc) · 1.36 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
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
int largestRectangleArea(vector<int>& A) {
int n = A.size(),i;
stack<int> st;
int res = 0,top,area;
for(i=0; i<n; i++){
if(st.empty() || A[st.top()]<=A[i]){
st.push(i);
continue;
}
while(!st.empty() && A[i]<A[st.top()]){
top = st.top();
st.pop();
area = A[top]*(st.empty() ? i: i-1-st.top());
res = max(res,area);
}
st.push(i);
}
while(!st.empty()){
top = st.top();
st.pop();
area = A[top]*(st.empty() ? i: i-1-st.top());
res = max(res,area);
}
return res;
}
};
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
if(n==0)
return 0;
if(n==1)
return heights[0];
heights.push_back(0);
stack<int> st;
st.push(-1);
int res = 0, curr, top;
for(int i=0; i<=n; i++){
while(st.top()!=-1 && heights[st.top()]>heights[i]){
top = st.top();st.pop();
curr = heights[top]*(i-st.top()-1);
if(res<curr)
res = curr;
}
st.push(i);
}
return res;
}
};