-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0084.cpp
More file actions
28 lines (28 loc) · 1011 Bytes
/
Copy path0084.cpp
File metadata and controls
28 lines (28 loc) · 1011 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
class Solution {
public:
int largestRectangleArea(vector<int> &heights) {
vector<pair<int,int>> s; // index, height
s.push_back({0, heights[0]});
int maxArea = 0;
for (int i = 1; i < heights.size(); i++) {
if (heights[i] >= s.back().second) {
s.push_back({i, heights[i]});
} else {
int startIndex = i;
while (s.size() && heights[i] < s.back().second) {
int width = i - s.back().first, height = s.back().second;
maxArea = max(maxArea, width * height);
startIndex = s.back().first;
s.pop_back();
}
s.push_back({startIndex, heights[i]});
}
}
while (s.size()) {
int width = heights.size() - s.back().first, height = s.back().second;
maxArea = max(maxArea, height * width);
s.pop_back();
}
return maxArea;
}
};