forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRectangleInHistogram.java
More file actions
51 lines (40 loc) · 1.07 KB
/
LargestRectangleInHistogram.java
File metadata and controls
51 lines (40 loc) · 1.07 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
package Stacks;
import java.util.ArrayList;
import java.util.Stack;
/**
* Author - archit.s
* Date - 25/10/18
* Time - 1:07 PM
*/
public class LargestRectangleInHistogram {
public int largestRectangleArea(ArrayList<Integer> A) {
Stack<Integer> s = new Stack<>();
int i=0;
int maxArea = 0;
int tempArea = 0;
while(i<A.size()){
int current = A.get(i);
if(s.empty() || A.get(s.peek()) <= current ){
s.push(i);
i++;
}
else{
int top = s.peek();
s.pop();
tempArea = A.get(top) * (s.empty() ? i : (i-s.peek()-1));
if(maxArea < tempArea){
maxArea = tempArea;
}
}
}
while(!s.empty()){
int top = s.peek();
s.pop();
tempArea = A.get(top) * (s.empty() ? i : (i-s.peek()-1));
if(maxArea < tempArea){
maxArea = tempArea;
}
}
return maxArea;
}
}