Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Largest Rectangle in Histogram problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Largest Rectangle in Histogram problem solution we have 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.

Leetcode Largest Rectangle in Histogram problem solution

Leetcode Largest Rectangle in Histogram problem solution in Python.

class Solution:
    def largestRectangleArea(self, arr: List[int]) -> int:
        stack,pos,maxA=[],0,0
        n=len(arr)
        for i in range(n):
            
            while(stack and arr[stack[-1]]>arr[i]):
                pos= stack.pop()
                if stack:
                    area=arr[pos]*(i-stack[-1]-1)
                    maxA=max(maxA,area)
                else:
                    area=arr[pos]*(i-0)
                    maxA=max(maxA,area)
                    
                    
            stack.append(i)
          
        while stack:
                
                pos= stack.pop()
                if stack:
                    area=arr[pos]*(n-stack[-1]-1)
                    maxA=max(maxA,area)
                else:
                    area=arr[pos]*(n-0)
                    maxA=max(maxA,area)
                
        
        return maxA

Largest Rectangle in Histogram problem solution in Java.

class Solution {
    public int largestRectangleArea(int[] heights) {
        Stack < Integer > stack = new Stack < > ();
        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        Arrays.fill(right,heights.length);
        Arrays.fill(left,-1);
        for(int i = 0;i<heights.length;i++){
            while(!stack.empty()&&heights[stack.peek()]>heights[i]){
                right[stack.peek()] = i;
                stack.pop();
            }
            stack.push(i);
        }
        
        stack = new Stack<>();
        for(int i = heights.length-1;i>=0;i--){
            while(!stack.empty()&&heights[stack.peek()]>heights[i]){
                left[stack.peek()] = i;
                stack.pop();
            }
            stack.push(i);
        }
        
        
        int maxArea = 0;
        for (int i = 0; i < heights.length; ++i) {
            maxArea = Math.max(maxArea,(right[i]-left[i]-1)*heights[i]);
        }
        
        return maxArea;
    }
}

Problem solution in C++.

int largestRectangleArea(vector<int>& arr) {
        int n=arr.size();
        if(n==0) return 0;
        stack<int> s;
        int i=0,area=INT_MIN;
        while(i<n)
        {
            if(s.empty()||arr[s.top()]<=arr[i])
            {
                s.push(i);i++;
            }
            else
            {
                int num=s.top();s.pop();
                int l=s.empty()?-1:s.top();
                area=max(area,arr[num]*(i-l-1));
            }
        }
        while(!s.empty())
        {
            int num=s.top();s.pop();
                int l=s.empty()?-1:s.top();
                area=max(area,arr[num]*(i-l-1));
        }
        return area;
    }

Problem solution in C.

struct tagStackObj{
    int height;
    int width;
};

int largestRectangleArea(int* heights, int heightsSize){
    int maxSize = 0;
    int squareSize = 0;
    int i;
    int *p = heights;
    int index = 0;
    int stride = 0;
    struct tagStackObj *pStack = NULL;
    
    if (0 == heightsSize)
        return 0;
    
    pStack = malloc(heightsSize * sizeof(struct tagStackObj));
    pStack[index].width = 1;
    pStack[index].height = heights[0];
    
    for (i = 1; i < heightsSize; i++){
        if (p[i] >= pStack[index].height){
            index++;
            pStack[index].height = p[i];
            pStack[index].width = 1;
        }
        else{
            stride = 0;
            while ((index >= 0) && (pStack[index].height > p[i])){
                stride += pStack[index].width;
                squareSize = stride * pStack[index].height;
                maxSize = squareSize > maxSize ? squareSize : maxSize;
                index--;
            }
            
            pStack[++index].height = p[i];
            pStack[index].width = stride + 1;
        }
    }
    
    stride = 0;
    while ((index >= 0) && (pStack[index].height > -1)){
        stride += pStack[index].width;
        squareSize = stride * pStack[index].height;
        maxSize = squareSize > maxSize ? squareSize : maxSize;
        index--;
    }
    
    return maxSize;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes