import java.util.*;
public class Solution {
int len = heights.size();
int rb[] = new int [len];
Stack<Integer> st = new Stack<>();
// calculating right boundary
for (int i = len-1; i >= 0; i--) {
int cur = heights.get(i);
while (!st.isEmpty() && heights.get(st.peek()) >= cur)
st.pop();
if (st.isEmpty() == true)
rb[i] = len;
else
rb[i] = st.peek();
st.push(i);
}
int lb[] = new int [len];
st = new Stack<>();
// calculating left boundary
for (int i = 0; i < len; i++) {
int cur = heights.get(i);
while (!st.isEmpty() && heights.get(st.peek()) >= cur)
st.pop();
if (st.isEmpty() == true)
lb[i] = -1;
else
lb[i] = st.peek();
st.push(i);
}
// calcution of max Area
int maxArea = 0;
for (int i = 0; i < len; i++) {
int width = rb[i] - lb[i] - 1;
int curArea = width * heights.get(i);
maxArea
= Math.
max(maxArea, curArea
); }
return maxArea;
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIFNvbHV0aW9uIHsKICBwdWJsaWMgc3RhdGljIGludCBsYXJnZXN0UmVjdGFuZ2xlKEFycmF5TGlzdCA8IEludGVnZXIgPiBoZWlnaHRzKSB7CiAgICAKCQlpbnQgbGVuID0gaGVpZ2h0cy5zaXplKCk7CiAgICAgICAgCiAgICAgICAgaW50IHJiW10gPSBuZXcgaW50IFtsZW5dOwogICAgICAgIAogICAgICAgIAogICAgICAgIFN0YWNrPEludGVnZXI+IHN0ID0gbmV3IFN0YWNrPD4oKTsKICAgICAgICAKICAgICAgICAKICAgICAgICAvLyBjYWxjdWxhdGluZyByaWdodCBib3VuZGFyeQogICAgICAgIAogICAgICAgIGZvciAoaW50IGkgPSBsZW4tMTsgaSA+PSAwOyBpLS0pIHsKICAgICAgICAgICAgCiAgICAgICAgICAgIGludCBjdXIgPSBoZWlnaHRzLmdldChpKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIHdoaWxlICghc3QuaXNFbXB0eSgpICYmIGhlaWdodHMuZ2V0KHN0LnBlZWsoKSkgPj0gY3VyKQogICAgICAgICAgICAgICAgc3QucG9wKCk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAoc3QuaXNFbXB0eSgpID09IHRydWUpCiAgICAgICAgICAgICAgICByYltpXSA9IGxlbjsKICAgICAgICAgICAgCiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHJiW2ldID0gc3QucGVlaygpOwogICAgICAgICAgICAKICAgICAgICAgICAgc3QucHVzaChpKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgCiAgICAgICAgCiAgICAgICAgaW50IGxiW10gPSBuZXcgaW50IFtsZW5dOwogICAgICAgIAogICAgICAgIAogICAgICAgIHN0ID0gbmV3IFN0YWNrPD4oKTsKICAgICAgICAKICAgICAgICAvLyBjYWxjdWxhdGluZyBsZWZ0IGJvdW5kYXJ5CiAgICAgICAgCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgICAKICAgICAgICAgICAgaW50IGN1ciA9IGhlaWdodHMuZ2V0KGkpOwogICAgICAgICAgICAKICAgICAgICAgICAgd2hpbGUgKCFzdC5pc0VtcHR5KCkgJiYgaGVpZ2h0cy5nZXQoc3QucGVlaygpKSA+PSBjdXIpCiAgICAgICAgICAgICAgICBzdC5wb3AoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmIChzdC5pc0VtcHR5KCkgPT0gdHJ1ZSkKICAgICAgICAgICAgICAgIGxiW2ldID0gLTE7CiAgICAgICAgICAgIAogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBsYltpXSA9IHN0LnBlZWsoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIHN0LnB1c2goaSk7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIAogICAgICAgIC8vIGNhbGN1dGlvbiBvZiBtYXggQXJlYQogICAgICAgIAogICAgICAgIGludCBtYXhBcmVhID0gMDsKICAgICAgICAKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICAgIAogICAgICAgICAgICBpbnQgd2lkdGggPSByYltpXSAtIGxiW2ldIC0gMTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGludCBjdXJBcmVhID0gd2lkdGggKiBoZWlnaHRzLmdldChpKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIG1heEFyZWEgPSBNYXRoLm1heChtYXhBcmVhLCBjdXJBcmVhKTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgCiAgICAgICAgcmV0dXJuIG1heEFyZWE7CiAgfQp9