-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode84.cpp
More file actions
59 lines (33 loc) · 984 Bytes
/
leetcode84.cpp
File metadata and controls
59 lines (33 loc) · 984 Bytes
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
55
56
57
58
59
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> right(n,0);
vector<int> left(n,0);
stack<int> s;
for(int i=n-1 ; i>=0 ; i--){
while(s.size() > 0 && heights[s.top()] >= heights[i]){
s.pop();
}
right[i] = s.empty() ? n : s.top();
s.push(i);
}
while(!s.empty()){
s.pop();
}
for(int i=0 ; i<n ; i++ ){
while(s.size()>0 && heights[s.top()]>= heights[i]){
s.pop();
}
left[i] = s.empty() ? -1 : s.top();
s.push(i);
}
int ans =0 ;
for(int i = 0 ; i<n; i++ ){
int width = right[i] - left[i]-1;
int currarea = heights[i] * width;
ans = max(currarea , ans);
}
return ans;
}
};