-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxAreaHistogram.cpp
56 lines (55 loc) · 1.55 KB
/
maxAreaHistogram.cpp
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
#include<iostream>
#include<vector>
#include<stack>
#include<sstream>
using namespace std;
long int maxRectangle(vector<int>& histogram) {
long int maxArea = 0;
long int topRectangleArea = 0;
long int index = 0;
long int topRectangle;
stack<int> pendingRectangles;
while (index < histogram.size()) {
if (pendingRectangles.empty() || (histogram[index] >= histogram[pendingRectangles.top()])) {
pendingRectangles.push(index++);
} else {
topRectangle = pendingRectangles.top();
pendingRectangles.pop();
topRectangleArea = histogram[topRectangle] * (pendingRectangles.empty()?index:(index - pendingRectangles.top() - 1));
if (topRectangleArea > maxArea)
maxArea = topRectangleArea;
}
}
while (pendingRectangles.empty()) {
topRectangle = pendingRectangles.top();
pendingRectangles.pop();
topRectangleArea = histogram[topRectangle] * (pendingRectangles.empty()?index:(index - pendingRectangles.top() - 1));
if (topRectangleArea > maxArea)
maxArea = topRectangleArea;
}
return maxArea;
}
int main(int argc, char* argv[]) {
vector<int> histogram;
if (argc > 1) {
int values = argc - 1;
int index = 1;
while (index < argc) {
istringstream ss(argv[index]);
int value;
if (ss >> value) {
histogram.push_back(value);
} else {
cout << "Invalid parameter ! " << argv[index] << endl;
}
index++;
}
} else {
histogram.push_back(4);
histogram.push_back(4);
histogram.push_back(2);
histogram.push_back(1);
}
cout << "Max rectangle area in given histogram is " << maxRectangle(histogram) << endl;
return 0;
}