Stack
Largest Rectangle in Histogram
DESCRIPTION (inspired by 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:
heights = [2,8,5,6,2,3]
Output:
15
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
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n) where n is the length of the input array. Each item is pushed and popped from the stack at most once.
Space Complexity: O(n) where n is the length of the input array for the size of the stack.
Mark as read
Unlock Premium Coding Content
On This Page
Your account is free and you can post anonymously if you choose.