84. Largest Rectangle in Histogram LeetCode Solution

In this guide, you will get 84. Largest Rectangle in Histogram LeetCode Solution with the best time and space complexity. The solution to Largest Rectangle in Histogram problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Largest Rectangle in Histogram solution in C++
  4. Largest Rectangle in Histogram solution in Java
  5. Largest Rectangle in Histogram solution in Python
  6. Additional Resources
84. Largest Rectangle in Histogram LeetCode Solution image

Problem Statement of Largest Rectangle in Histogram

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 <= 105
0 <= heights[i] <= 104

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

84. Largest Rectangle in Histogram LeetCode Solution in C++

class Solution {
 public:
  int largestRectangleArea(vector<int>& heights) {
    int ans = 0;
    stack<int> stack;

    for (int i = 0; i <= heights.size(); ++i) {
      while (!stack.empty() &&
             (i == heights.size() || heights[stack.top()] > heights[i])) {
        const int h = heights[stack.top()];
        stack.pop();
        const int w = stack.empty() ? i : i - stack.top() - 1;
        ans = max(ans, h * w);
      }
      stack.push(i);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

84. Largest Rectangle in Histogram LeetCode Solution in Java

class Solution {
  public int largestRectangleArea(int[] heights) {
    int ans = 0;
    Deque<Integer> stack = new ArrayDeque<>();

    for (int i = 0; i <= heights.length; ++i) {
      while (!stack.isEmpty() && (i == heights.length || heights[stack.peek()] > heights[i])) {
        final int h = heights[stack.pop()];
        final int w = stack.isEmpty() ? i : i - stack.peek() - 1;
        ans = Math.max(ans, h * w);
      }
      stack.push(i);
    }

    return ans;
  }
}
// code provided by PROGIEZ

84. Largest Rectangle in Histogram LeetCode Solution in Python

class Solution:
  def largestRectangleArea(self, heights: list[int]) -> int:
    ans = 0
    stack = []

    for i in range(len(heights) + 1):
      while stack and (i == len(heights) or heights[stack[-1]] > heights[i]):
        h = heights[stack.pop()]
        w = i - stack[-1] - 1 if stack else i
        ans = max(ans, h * w)
      stack.append(i)

    return ans
# code by PROGIEZ

Additional Resources

See also  144. Binary Tree Preorder Traversal LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.