💥[EXPLAINED] TypeScript. Runtime beats 99.47%
Intuition\nUse a stack to efficiently track the indices of histogram bars. The stack helps determine the width of rectangles formed with each bar as the shortes
# Intuition\nUse a stack to efficiently track the indices of histogram bars. The stack helps determine the width of rectangles formed with each bar as the shortest bar in the rectangle.\n\n# Approach\nUse a stack to track bar indices. For each bar, pop from the stack when encountering a shorter bar, calculating the maximum rectangle area with the popped bar as the smallest height. Finally, pop remaining bars to compute the area.\n\n# Complexity\n- Time complexity:\nO(n), as each bar is pushed and popped from the stack exactly once.\n\n- Space complexity:\nO(n), for the stack used to store indices.\n\n# Code\n```typescript []\nfunction largestRectangleArea(heights: number[]): number {\n    const stack: number[] = [];\n    let maxArea = 0;\n    let index = 0;\n\n    while (index < heights.length) {\n        if (stack.length === 0 || heights[stack[stack.length - 1]] <= heights[index]) {\n            stack.push(index++);\n        } else {\n            const topOfStack = stack.pop()!;\n            const height = heights[topOfStack];\n            const width = stack.length === 0 ? index : index - stack[stack.length - 1] - 1;\n            maxArea = Math.max(maxArea, height * width);\n        }\n    }\n\n    while (stack.length > 0) {\n        const topOfStack = stack.pop()!;\n        const height = heights[topOfStack];\n        const width = stack.length === 0 ? index : index - stack[stack.length - 1] - 1;\n        maxArea = Math.max(maxArea, height * width);\n    }\n\n    return maxArea;\n}\n\n```
['TypeScript']