Largest Rectangle In Histogram
Detailed guide and Python implementation for the 'Largest Rectangle In Histogram' problem.
Problem Statement
Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Write a function largestRectangleArea(heights: List[int]) -> int.
- •1 <= len(heights) <= 10^5
- •0 <= heights[i] <= 10^4
Examples
heights = [2, 1, 5, 6, 2, 3]
10
The largest rectangle has area 10, formed by bars at index 2 and 3 (heights 5 and 6), with width 2 and height 5.
heights = [2, 4]
4
The largest rectangle is the bar at index 1 with height 4 and width 1, giving area 4.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Try/Except & Error Handling
Prevent your Python scripts from crashing. Learn try, except, finally blocks and how to raise custom exceptions properly.
How to Reverse a String in Python
Learn how to reverse a string in Python using slicing, the reversed() function, and loop concatenation with visual code examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.