[JAVA] Clean Code, O(N) Solution Using Stack Data Structure
\nclass Solution {\n    public int largestRectangleArea(int[] heights) {\n        \n        int maximumRectangle = 0;\n        Stack<Integer> stack = new Stack<
```\nclass Solution {\n    public int largestRectangleArea(int[] heights) {\n        \n        int maximumRectangle = 0;\n        Stack<Integer> stack = new Stack<> ();\n        \n        for (int i = 0; i <= heights.length; i++) {\n            while (!stack.isEmpty () && (i == heights.length || heights[stack.peek ()] > heights[i])) {\n                int length = heights[stack.pop ()];\n                int width = stack.isEmpty () ? i : i - stack.peek () - 1;\n                maximumRectangle = Math.max (maximumRectangle, length * width);\n            }\n            \n            stack.push (i);\n        }\n        \n        return maximumRectangle;\n    }\n}\n```\n\nPlease help to **UPVOTE** if this post is useful for you.\nIf you have any questions, feel free to comment below.\n**HAPPY CODING :)\nLOVE CODING :)**\n
['Stack', 'Java']