Skip to content

Commit 0cc5930

Browse files
committed
Added Largest Rectangle in Histogram
1 parent ec507d1 commit 0cc5930

4 files changed

+113
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <vector>
2+
#include <stack>
3+
#include <algorithm>
4+
5+
class Solution {
6+
public:
7+
int largestRectangleArea(std::vector<int>& heights) {
8+
int n = heights.size();
9+
std::stack<std::pair<int, int>> stk;
10+
int maxArea = 0;
11+
12+
for (int i = 0; i < n; i++) {
13+
int height = heights[i];
14+
int start = i;
15+
while (!stk.empty() && height < stk.top().first) {
16+
auto [h, j] = stk.top(); stk.pop();
17+
int w = i - j;
18+
maxArea = std::max(maxArea, h * w);
19+
start = j;
20+
}
21+
stk.push({height, start});
22+
}
23+
24+
while (!stk.empty()) {
25+
auto [h, j] = stk.top(); stk.pop();
26+
int w = n - j;
27+
maxArea = std::max(maxArea, h * w);
28+
}
29+
30+
return maxArea;
31+
}
32+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int largestRectangleArea(int[] heights) {
3+
int n = heights.length;
4+
Stack<int[]> stk = new Stack<>();
5+
int maxArea = 0;
6+
7+
for (int i = 0; i < n; i++) {
8+
int height = heights[i];
9+
int start = i;
10+
while (!stk.isEmpty() && height < stk.peek()[0]) {
11+
int[] popped = stk.pop();
12+
int h = popped[0];
13+
int j = popped[1];
14+
int w = i - j;
15+
maxArea = Math.max(maxArea, h * w);
16+
start = j;
17+
}
18+
stk.push(new int[]{height, start});
19+
}
20+
21+
while (!stk.isEmpty()) {
22+
int[] popped = stk.pop();
23+
int h = popped[0];
24+
int j = popped[1];
25+
int w = n - j;
26+
maxArea = Math.max(maxArea, h * w);
27+
}
28+
29+
return maxArea;
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var largestRectangleArea = function(heights) {
2+
var n = heights.length;
3+
var stk = [];
4+
var maxArea = 0;
5+
6+
for (var i = 0; i < n; i++) {
7+
var height = heights[i];
8+
var start = i;
9+
while (stk.length > 0 && height < stk[stk.length - 1][0]) {
10+
var [h, j] = stk.pop();
11+
var w = i - j;
12+
var a = h * w;
13+
maxArea = Math.max(maxArea, a);
14+
start = j;
15+
}
16+
stk.push([height, start]);
17+
}
18+
19+
while (stk.length > 0) {
20+
var [h, j] = stk.pop();
21+
var w = n - j;
22+
maxArea = Math.max(maxArea, h * w);
23+
}
24+
25+
return maxArea;
26+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def largestRectangleArea(self, heights: List[int]) -> int:
3+
n = len(heights)
4+
stk = []
5+
max_area = 0
6+
7+
for i, height in enumerate(heights):
8+
start = i
9+
while stk and height < stk[-1][0]:
10+
h, j = stk.pop()
11+
w = i - j
12+
a = h*w
13+
max_area = max(max_area, a)
14+
start = j
15+
stk.append((height, start))
16+
17+
while stk:
18+
h, j = stk.pop()
19+
w = n - j
20+
max_area = max(max_area, h*w)
21+
22+
return max_area
23+
# Time: O(n)
24+
# Space: O(n)

0 commit comments

Comments
 (0)