-
Notifications
You must be signed in to change notification settings - Fork 22
/
FindLargestAreaRectangleInHistogram
58 lines (46 loc) · 1.37 KB
/
FindLargestAreaRectangleInHistogram
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
package FindLargestAreaRectangleInHistogram;
import java.util.Scanner;
import java.util.Stack;
public class UsingStackToStoreIndices {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
System.out.println("Enter the height of each index in the histogram");
System.out.println("Enter the size of the array");
int n = in.nextInt();
int[] hist = new int[n];
System.out.println("Enter the height of the histogram rectangles");
for(int i=0;i<hist.length;i++)
hist[i]=in.nextInt();
System.out.println("The maximum area rectangle in the histogram is: "+largestRectangleArea(hist));
}
finally{
in.close();
}
}
public static int largestRectangleArea(int[] height) {
if ( height==null||height.length==0){
return 0;
}
Stack<Integer> stack=new Stack<Integer>();
int max=0;
int i=0;
while(i<height.length){
if (stack.isEmpty()||height[i]>=height[stack.peek()]){
stack.push(i);
i++;
}
else{
int h=height[stack.pop()];
int wid=stack.isEmpty()?i:i-stack.peek()-1;
max=Math.max(h*wid, max);
}
}
while (!stack.isEmpty()){
int h=height[stack.pop()];
int wid=stack.isEmpty()?i:i-stack.peek()-1;
max=Math.max(h*wid, max);
}
return max;
}
}