-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.java
More file actions
39 lines (39 loc) · 984 Bytes
/
Copy pathDay3.java
File metadata and controls
39 lines (39 loc) · 984 Bytes
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
import java.util.*;
public class Day3{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of bars in the histogram");
int size = in.nextInt();
System.out.println("Enter the sizes of the histogram");
int hist[] = new int[size];
for(int i=0;i<size;i++){
hist[i] = in.nextInt();
}
System.out.println("The max area of the rectangle in the histogram is "+maxArea(hist));
}
static int maxArea(int hist[]){
Stack<Integer> s = new Stack<Integer>();
int max=0,i=0,area=0,currMax=0;
while(i<hist.length){
if (s.isEmpty()||hist[s.peek]()<hist[i]) {
s.push(i);
i++;
}
else{
currMax = s.pop();
area = hist[currMax] * (s.isEmpty() ? i : i-1-s.peek());
if (area>max) {
max = area;
}
}
}
while(!s.isEmpty()){
currMax = s.pop();
area = hist[currMax] * (s.isEmpty() ? i : i-1-s.peek());
if (area>max) {
max = area;
}
}
return max;
}
}