# Largest Rectangle in Histogram LeetCode Solution

Here, We see Largest Rectangle in Histogram problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## 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: (fig-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: (fig-2)
Input: heights = [2,4]
Output: 4```

## Largest Rectangle in Histogram Leetcode Solution C++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> st;
int ans=0;
heights.push_back(0);
for(int i=0;i<heights.size();i++){
while(!st.empty() && heights[st.top()]>heights[i]){
int top=heights[st.top()];
st.pop();
int ran=st.empty()?-1:st.top();
ans=max(ans,top*(i-ran-1));
}
st.push(i);
}
return ans;
}
};
```Code language: C++ (cpp)```

## Largest Rectangle in Histogram Leetcode Solution Java ->

``````class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
int[] lessFromLeft = new int[heights.length]; // idx of the first bar the left that is lower than current
int[] lessFromRight = new int[heights.length]; // idx of the first bar the right that is lower than current
lessFromRight[heights.length - 1] = heights.length;
lessFromLeft = -1;

for (int i = 1; i < heights.length; i++) {
int p = i - 1;
while (p >= 0 && heights[p] >= heights[i]) {
p = lessFromLeft[p];
}
lessFromLeft[i] = p;
}
for (int i = heights.length - 2; i >= 0; i--) {
int p = i + 1;

while (p < heights.length && heights[p] >= heights[i]) {
p = lessFromRight[p];
}
lessFromRight[i] = p;
}
int maxArea = 0;
for (int i = 0; i < heights.length; i++) {
maxArea = Math.max(maxArea, heights[i] * (lessFromRight[i] - lessFromLeft[i] - 1));
}
return maxArea;
}
}
```Code language: Java (java)```

## Largest Rectangle in Histogram Leetcode Solution JavaScript ->

``````var largestRectangleArea = function(heights) {
heights.push(0)
const stack = [];
let maxArea = 0, curr, currH, top, topH, area;

for(let i = 0; i < heights.length; i++) {
top = stack[stack.length-1];
topH = heights[top];
while(stack.length > 1 && topH > heights[i]) {
curr = stack.pop();
currH = heights[curr];
top = stack[stack.length-1];
topH = heights[top];
area = currH * (i - 1 - top);
maxArea = Math.max(area, maxArea);
}
if(stack.length && topH > heights[i]) {
curr = stack.pop();
currH = heights[curr];
area = currH * i;
maxArea = Math.max(area, maxArea);
}
stack.push(i);
}
return maxArea;
};
```Code language: JavaScript (javascript)```

## Largest Rectangle in Histogram Leetcode Solution Python ->

``````class Solution(object):
def largestRectangleArea(self, heights):
heights.append(0)
stack = [-1]
ans = 0
for i in xrange(len(heights)):
while heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
ans = max(ans, h * w)
stack.append(i)
heights.pop()
return ans
```Code language: Python (python)```

#### Search in Rotated Sorted Array LeetCode Solution

Given the array nums after the possible rotation and an integer target, return the index…

#### Single Number LeetCode Solution

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

#### Group Anagrams LeetCode Solution

Given an array of strings strs, group the anagrams together. You can return the answer…

#### Add Two Numbers LeetCode Solution

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order,…

#### Majority Element LeetCode Solution

Given an array nums of size n, return the majority element. The majority element is…

#### Delete Node in a Linked List LeetCode Solution

There is a singly-linked list head and we want to delete a node node in…