Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Largest Rectangle in Histogram problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes