forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path84-Largest-Rectangle-in-Histogram.java
More file actions
40 lines (39 loc) · 1.37 KB
/
84-Largest-Rectangle-in-Histogram.java
File metadata and controls
40 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
int[] leftMin = new int[n];
Stack<Integer> left = new Stack<>();
Arrays.fill(leftMin, -1);
for (int i = heights.length - 1; i >= 0; i--) {
if (left.isEmpty() || heights[i] >= heights[left.peek()]) {
left.add(i);
} else {
while (!left.isEmpty() && heights[left.peek()] > heights[i]) {
leftMin[left.peek()] = i;
left.pop();
}
left.add(i);
}
}
int[] rightMin = new int[n];
Stack<Integer> right = new Stack<>();
Arrays.fill(rightMin, heights.length);
for (int i = 0; i < heights.length; i++) {
if (right.isEmpty() || heights[i] >= heights[right.peek()]) {
right.add(i);
} else {
while (!right.isEmpty() && heights[right.peek()] > heights[i]) {
rightMin[right.peek()] = i;
right.pop();
}
right.add(i);
}
}
// System.out.println()
int area = Integer.MIN_VALUE;
for (int i = 0; i < heights.length; i++) {
area = Math.max(area, heights[i] * (rightMin[i] - leftMin[i] - 1));
}
return area;
}
}