-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangleinHistogram.cpp
More file actions
75 lines (66 loc) · 1.58 KB
/
LargestRectangleinHistogram.cpp
File metadata and controls
75 lines (66 loc) · 1.58 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
//
//
//Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
//
//
//The largest rectangle is shown in the shaded area, which has area = 10 unit.
//
//For example,
//Given heights = [2,1,5,6,2,3],
//return 10.
//
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
class LargestRectangleinHistogram {
public:
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
int n = heights.size();
stack<int> s;
int res = 0;
for (int i = 0; i<n; ++i){
while (!s.empty() && heights[i] <= heights[s.top()]){
int h = heights[s.top()];
s.pop();
int start = s.empty() ? -1 : s.top();
int area = (i - start - 1)*h;
if (area>res) res = area;
}
s.push(i);
}
return res;
/*stack<int> s;
heights.push_back(0);
int maxSize = 0;
for (int i = 0; i < heights.size(); i++){
if (s.empty() || heights[i] >= heights[s.top()]){
s.push(i);
}
else{
int temp = heights[s.top()];
s.pop();
maxSize = max(maxSize, temp * (s.empty() ? i : i - 1 - s.top()));
i--;
}
}
return maxSize;*/
}
/*stack<int> s;
heights.push_back(0);
int maxSize = 0;
for (int i = 0; i < heights.size(); i++){
if (s.empty() || heights[i] >= heights[s.top()]){
s.push(i);
}
else{
int temp = heights[s.top()];
s.pop();
maxSize = max(maxSize, temp * (s.empty() ? i : i - 1 - s.top()));
i--;
}
}
return maxSize;*/
};