forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest_rect_area_under_histogram.cpp
80 lines (69 loc) · 1.87 KB
/
Largest_rect_area_under_histogram.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Largest rectangular area under histogram
=========================================
Given the heights of the bars of a histogram, find out the largest
rectangular area under the histogram. Width of each bar is 1 unit.
Link: https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1
Time Complexity: O(n)
Space Complexity: O(n)
*/
#include<iostream>
#include <stack>
using namespace std;
// Prints largest rectangular area under histogram
void largestAreaUnderHistogram(int height[], int n) {
// Stores index of the bars
stack<int> s;
int ans = 0;
int l = 0, r = 0;
for (int i = 0; i < n; i++) {
// If stack is empty, push the bar
if (s.empty()) {
s.push(i);
continue;
}
// If height[i] is greater then push the bar
if (height[i] > height[s.top()]) {
s.push(i);
} else {
// I have encountered a height that is less than my last height
// Pop out all bars whose height is greater than height[i],
// Simulataneously calculate the new area
while (!s.empty() && height[i] < height[s.top()]) {
int toBeRemoved = s.top();
s.pop();
int area;
if (s.empty()) {
area = height[toBeRemoved] * i;
} else {
area = height[toBeRemoved] * (i - s.top() - 1);
}
//cout << toBeRemoved << " " << area << endl;
if (area > ans) {
ans = area;
l = (!s.empty()) ? s.top() : 0;
r = i;
}
ans = max(ans, area);
}
s.push(i);
}
}
cout << "Largest area = " << ans << " between bars " << l << " and " << r ;
}
int main() {
// Input number of bars
cout << "Enter number of bars: ";
int n; cin >> n;
// Input heights
cout << "Enter height of each bar: ";
int height[n];
for (int i = 0; i < n; i++) cin >> height[i];
largestAreaUnderHistogram(height, n);
}
/*
Console:
Enter number of bars: 7
Enter height of each bar: 6 2 5 4 5 1 6
Largest area = 12 between bars 1 and 5
*/