forked from Priyadarshan2000/Hacktoberfest_2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest_rectangle_in_Histogram.cpp
67 lines (60 loc) · 1.43 KB
/
Largest_rectangle_in_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
/*Problem statement:
Given an array of integers heights representing the histogram's bar height where the
width of each bar is 1, return the area of the largest rectangle in the histogram.
Input:
heights = [2,1,5,6,2,3]
Output: 10
*/
#include<bits/stdc++.h>
using namespace std;
int largestRectangleArea(vector<int>& heights) {
int n=heights.size();
vector<int> v;
stack<pair<int,pair<int,int>>> s;
v.push_back(heights[0]);
s.push({heights[0],{1,0}});
for(int i=1;i<n;i++){
int count =0;
while(s.size()!=0){
auto it=s.top();
if(it.first<=heights[i]){
break;
}
else{
auto at=it.second;
count=count+at.first;
v.push_back(it.first*(at.first+(i-at.second)-1));
s.pop();
}
}
v.push_back(heights[i]);
s.push({heights[i],{count+1,i}});
}
int x=0;
while(s.size()!=0){
auto it=s.top();
x=it.first;
auto at=it.second;
v.push_back(x*(at.first+(n-at.second)-1));
s.pop();
}
int max=0;
for(int j=0;j<v.size();j++){
if(max<v[j]){
max=v[j];
}
}
return max;
}
int main(){
// taking input
int n;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}
cout<<largestRectangleArea(v);
}