-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxAreaHistogram.CPP
More file actions
42 lines (34 loc) · 1.32 KB
/
Copy pathmaxAreaHistogram.CPP
File metadata and controls
42 lines (34 loc) · 1.32 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
int Solution::largestRectangleArea(vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
stack<int> indices;
A.push_back(0);
int max_area = 0;
int i=0;
while(i<A.size()){
if(indices.empty() || A[i] > A[indices.top()]){
indices.push(i);
++i;
}
else{
int top_index = indices.top();
int height = A[top_index];
// get rid of that bar by removing that index from stack and keep on popping untill A[i] is greater
indices.pop();
// find the width and multiply by lowest height
int area;
int width;
if(indices.empty()){
width = i;
}
else{
width = i - indices.top() - 1;
}
area = height*width;
max_area = (area > max_area)? area: max_area;
}
} // the stack is assured to be empty because of last 0
return max_area;
}