My C++ DP solution, 16ms, easy to understand!
I think it might be O(n), or very close to O(n), it cannot be O(n^2).\nCan anybody help to prove?\n\n\n    int largestRectangleArea(vector& height) {\n        i
I think it might be O(n), or very close to O(n), it cannot be O(n^2).\nCan anybody help to prove?\n\n\n    int largestRectangleArea(vector<int>& height) {\n        int n = height.size(), ans = 0, p;\n        vector<int> left(n,0), right(n,n);\n        for (int i = 1;i < n;++i) {\n            p = i-1;\n            while (p >= 0 && height[i] <= height[p])\n                p = left[p] - 1;\n            left[i] = p + 1;\n        }\n        for (int i = n-2;i >= 0;--i) {\n            p = i+1;\n            while (p < n && height[i] <= height[p])\n                p = right[p];\n            right[i] = p;\n        }\n        for (int i = 0;i < n;++i)\n            ans = max(ans,height[i]*(right[i]-left[i]));\n        return ans;\n    }
['C++']