16 ms beat 94% c++ solution
class Solution {\n    public:\n        int largestRectangleArea(vector<int>& heights) {\n            int n = heights.size(),Area=0;\n            vector<int> lef
    class Solution {\n    public:\n        int largestRectangleArea(vector<int>& heights) {\n            int n = heights.size(),Area=0;\n            vector<int> left(n,0),right(n,0);\n            for(int i=1;i<n;i++){ \n                int j = i-1;\n                while(j>=0 && heights[i]<=heights[j]){\n                    j -= (left[j]+1);\n                }\n                left[i] = i-j-1;  //starts from i, how far the rectangle can go toward left\n            }\n            for(int i=n-2;i>=0;i--){\n                int j=i+1;\n                while(j<n && heights[i]<=heights[j]){\n                    j += (right[j]+1);\n                }\n                right[i] = j-i-1; //starts from i, how far the rectangle can go toward right\n            }\n            for(int i=0;i<n;i++) Area = max(Area,heights[i]*(right[i]+1+left[i])); //Bottom: left[i]+right[i]+1\n            return Area;\n        }\n    };
[]