✔️ 100% Fastest Swift Solution, time: O(n), space: O(n).
\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the length of heights.\n    //   - space: O(n), where n is the length of heights.\n\
```\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the length of heights.\n    //   - space: O(n), where n is the length of heights.\n\n    func largestRectangleArea(_ heights: [Int]) -> Int {\n        var stack = [Int]()\n        let n = heights.count\n        var ans = 0\n        \n        for i in 0..<n {\n            while let curHeight = stack.last, heights[curHeight] >= heights[i] {\n                stack.removeLast()\n                let curWidth = stack.last ?? -1\n                ans = max(ans, heights[curHeight] * (i - curWidth - 1))\n            }\n            stack.append(i)\n        }\n        \n        while !stack.isEmpty {\n            let curHeight = stack.removeLast()\n            let curWidth = stack.last ?? -1\n            ans = max(ans, heights[curHeight] * (n - curWidth - 1))\n        }\n        \n        return ans\n    }\n\n}\n```\n\nLet me know in comments if you have any doubts. I will be happy to answer.\n\nPlease upvote if you found the solution useful.
['Swift']