-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxAreaInHistogram.java
56 lines (50 loc) · 1.56 KB
/
MaxAreaInHistogram.java
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
package Stacks;
import java.util.*;
public class MaxAreaInHistogram {
public static int largestRectangleArea(int[] heights) {
int maxArea = 0;
Stack<Integer> stack1 = new Stack<>();
int n = heights.length;
// next smaller right
int[] nsr = new int[n];
for (int i=n-1;i>=0;i--){
int curr = heights[i];
while (!stack1.isEmpty() && curr <= heights[stack1.peek()]){
stack1.pop();
}
if(stack1.isEmpty()) {
nsr[i] = n;
}else{
nsr[i] = stack1.peek();
}
stack1.push(i);
}
Stack<Integer> stack2 = new Stack<>();
// next smaller left
int[] nsl = new int[n];
for (int i=0;i<n;i++){
int curr = heights[i];
while (!stack2.isEmpty() && curr <= heights[stack2.peek()]){
stack2.pop();
}
if(stack2.isEmpty()) {
nsl[i] = -1;
}else{
nsl[i] = stack2.peek();
}
stack2.push(i);
}
// current area calculation
for (int i=0; i<n; i++){
int h = heights[i];
int w = nsr[i] - nsl[i] - 1;
int currArea = h * w;
maxArea = Math.max(maxArea, currArea);
}
return maxArea;
}
public static void main(String[] args) {
int[] arr = {2,1,5,6,2,3};
System.out.println(largestRectangleArea(arr));
}
}