Learn DSA
Depth-First Search
Greedy Algorithms
Stack
Largest Rectangle in Histogram
hard
DESCRIPTION (credit Leetcode.com)
Given an integer array heights representing the heights of histogram bars, write a function to find the largest rectangular area possible in a histogram, where each bar's width is 1.
Inputs:
Output:
💻 Desktop Required
The code editor works best on larger screens. Please open this page on your computer to write and run code.
"Write a function that takes an array of integers `heights` representing the heights of bars in a histogram and returns the area of the largest rectangle in the histogram."
Run your code to see results here
Have suggestions or found something wrong?
Explanation
Largest Rectangle at each Index
Brute-Force Solution
def largestRectangleArea(heights):max_area = 0n = len(heights)for i in range(n):left = i - 1while left >= 0 and heights[left] >= heights[i]:left -= 1right = i + 1while right < n and heights[right] >= heights[i]:right += 1max_area = max(max_area, (right - left - 1) * heights[i])return max_area
Monotonically Increasing Stack
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
largest rectangle in histogram
0 / 1
Pushing to the Stack
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
initialize variables
0 / 2
Popping from the Stack
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
push to stack
0 / 1
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
push to stack
0 / 2
Emptying the Stack
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
push to stack
0 / 4
Solution
def largest_rectangle_area(heights):stack = []max_area = 0i = 0while i < len(heights):if not stack or heights[i] >= heights[stack[-1]]:stack.append(i)i += 1else:top = stack.pop()right = i - 1left = stack[-1] if stack else -1area = heights[top] * (right - left)max_area = max(max_area, area)while stack:top = stack.pop()width = i - stack[-1] - 1 if stack else iarea = heights[top] * widthmax_area = max(max_area, area)return max_area
largest rectangle in histogram
0 / 14
Complexity Analysis
Login to track your progress
Your account is free and you can post anonymously if you choose.