Back to Practice Dashboard
Top 150 InterviewEasy

Largest Rectangle In Histogram

Learn how to solve the 'Largest Rectangle In Histogram' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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.

Constraints
  • 1 <= len(heights) <= 10^5
  • 0 <= heights[i] <= 10^4

Examples

Example 1
Input
heights = [2, 1, 5, 6, 2, 3]
Output
10
Explanation

The largest rectangle has area 10, formed by bars at index 2 and 3 (heights 5 and 6), with width 2 and height 5.

Example 2
Input
heights = [2, 4]
Output
4
Explanation

The largest rectangle is the bar at index 1 with height 4 and width 1, giving area 4.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor