package algorithmbasic.basicsets.class26;
import java.util.Stack;
public class LargestRectangleInHistogram {
public static int largestRectangleArea1(int[] height) {
Stack<Integer> stack = new Stack<>();
int MAX = Integer.MIN_VALUE;
for (int i = 0; i < height.length; i++) {
while (!stack.isEmpty() && height[i] < height[stack.peek()]) {
int j = stack.pop();
int leftBorder = stack.isEmpty() ? -1 : stack.peek();
int rightBorder = i;
MAX = Math.max(MAX, (rightBorder - leftBorder - 1) * height[j]);
}
stack.push(i);
}
while(!stack.isEmpty()) {
int j = stack.pop();
int leftBorder = stack.isEmpty() ? -1 : stack.peek();
int rightBorder = height.length;
MAX = Math.max(MAX, (rightBorder - leftBorder - 1) * height[j]);
}
return MAX;
}
}