forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest_Rectangle.cpp
More file actions
64 lines (59 loc) · 2.4 KB
/
Largest_Rectangle.cpp
File metadata and controls
64 lines (59 loc) · 2.4 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
for(int i=0; i<n; i++)
cin>>a[i];
/* brute force */
// int ans=0;
// for(int i=0; i<n; i++){
// int height=INT_MAX;
// for(int j=i; j<n; j++){
// height = min(a[j],height);
// int length = j-i+1;
// ans = max(ans, length*height);
// }
// }
// cout<<ans<<endl;
/* stack approach */
stack<int> s;
//we push -1 to the stack because for some elements there will be no previous
//smaller element in the array and we can store -1 as the index for previous smaller.
s.push(-1);
int area = a[0];
int i = 0;
//We declare left_smaller and right_smaller array of size n and initialize them with -1 and n as their default value.
//left_smaller[i] will store the index of previous smaller element for ith element of the array.
//right_smaller[i] will store the index of next smaller element for ith element of the array.
vector<int> left_smaller(n, -1), right_smaller(n, n);
while(i<n){
while(!s.empty() && s.top()!=-1 && a[s.top()]>a[i]){
//if the current element is smaller than element with index stored on the
//top of stack then, we pop the top element and store the current element index
//as the right_smaller for the popped element.
right_smaller[s.top()] = i;
s.pop();
}
if(i>0 && a[i]==a[i-1]){
//we use this condition to avoid the unnecessary loop to find the left_smaller.
//since the previous element is same as current element, the left_smaller will always be the same for both.
left_smaller[i] = left_smaller[i-1];
}else{
//Element with the index stored on the top of the stack is always smaller than the current element.
//Therefore the left_smaller[i] will always be s.top().
left_smaller[i] = s.top();
}
s.push(i);
i++;
}
for(int j = 0; j<n; j++){
//here we find area with every element as the smallest element in their range and compare it with the previous area.
// in this way we get our max Area form this.
area = max(area, a[j]*(right_smaller[j]-left_smaller[j]-1));
}
cout<<area<<endl;
return 0;
}