-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#69.cc
37 lines (32 loc) · 945 Bytes
/
LeetCode#69.cc
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
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int N = height.size();
if(N==0) return 0;
vector<int> dpLeft(N);
vector<int> dpRight(N);
dpLeft[0]=1;
for(int i=1;i<N;i++){
int j = i-1;
while(j>=0 && height[j]>=height[i]){
j=j-dpLeft[j];
}
dpLeft[i]=i-j;
}
dpRight[N-1]=1;
for(int i=N-2;i>=0;i--){
int j=i+1;
while(j<N && height[j]>=height[i])
j=j+dpRight[j];
dpRight[i]=j-i;
}
int ret = 0;
for(int i=0;i<N;i++){
if(height[i]*(dpLeft[i]+dpRight[i]-1) > ret)
ret = height[i]*(dpLeft[i]+dpRight[i]-1);
}
return ret;
}
};