Advertisement

Largest Rectangle in Histogram - LeetCode 84 Solution

Largest Rectangle in Histogram - Complete Solution Guide

Largest Rectangle in Histogram is LeetCode problem 84, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Example 1: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. Example 2: Input: heights = [2,4] Output: 4 Constraints: 1 <= heights.length <= 10 5 0 <= heights[i] <= 10 4

Detailed Explanation

The problem asks us to find the largest rectangular area that can be formed within a histogram represented by an array of integers, `heights`. Each integer in the array represents the height of a bar, and the width of each bar is assumed to be 1. The goal is to find the maximum area of a rectangle that can be inscribed within these bars. The input is an array of non-negative integers, and the output is a single integer representing the largest possible rectangular area.

Solution Approach

The provided solution uses a monotonic stack to efficiently compute the largest rectangular area. The stack stores the indices of the bars in increasing order of their heights. The algorithm iterates through the `heights` array (with a 0 appended at the end as a sentinel value). If the current bar's height is less than the height of the bar at the top of the stack, it means we've found the right boundary for the rectangle whose height is the top of the stack. We then pop the top of the stack, calculate the area of the rectangle using the popped height and the width (calculated from the current index and the index at the new top of the stack), and update the `max_area` if necessary. The stack stores indices to allow width calculations using index differences.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack and push -1 onto it. This -1 serves as a sentinel value to handle the case when the stack becomes empty.
  2. Step 2: Append 0 to the `heights` array. This ensures that all elements in the stack are processed at the end.
  3. Step 3: Iterate through the `heights` array using index `i` and value `h`.
  4. Step 4: While the stack is not empty (excluding only the sentinel -1) and the current height `h` is less than or equal to the height at the index on the top of the stack, pop the top element from the stack.
  5. Step 5: Calculate the area of the rectangle using the popped height. The width is calculated as `i - stack[-1] - 1`, where `i` is the current index and `stack[-1]` is the index of the next smaller element to the left of the popped element.
  6. Step 6: Update `max_area` with the maximum of the current `max_area` and the calculated area.
  7. Step 7: Push the current index `i` onto the stack.
  8. Step 8: After iterating through the entire `heights` array, remove the appended 0. The `max_area` now contains the largest rectangular area.
  9. Step 9: Return the `max_area`.

Key Insights

  • Insight 1: The largest rectangle's height must be equal to one of the heights in the `heights` array. Therefore, we can iterate through each height and treat it as the potential height of the largest rectangle.
  • Insight 2: For each height, we need to find the leftmost and rightmost boundaries that extend to bars with heights greater than or equal to the current height. The width of the rectangle would be the distance between these boundaries minus one, and the area is then the height multiplied by this width.
  • Insight 3: A monotonic stack helps efficiently track the potential boundaries and find the previous and next smaller elements for each bar in the histogram.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack, Monotonic Stack.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Capital One, Cisco, DE Shaw, DoorDash, Flipkart, MAQ Software, Meta, Microsoft, Myntra, PhonePe, Roblox, Sprinklr, Uber, Walmart Labs, Yahoo, Zepto, Zoho, Zomato, Zynga, oyo, tcs.