84. Largest Rectangle in Histogram ( mono increasing stack
Last updated
Last updated
// 第二想到固定某柱子, 背向雙指針, 找左右邊界(找比自己高的), 求面積
class Solution {
public int largestRectangleArea(int[] heights) {
int max = 0;
int n = heights.length;
for (int i = 0; i < n ; i++) {
int curr = heights[i];
int left = i-1;
int right = i+1;
while (left >= 0 && heights[left] >= curr) {
left--;
}
while (right < n && heights[right] >= curr) {
right++;
}
max = Math.max(max, curr*(right - left - 1));
}
return max;
}
}while (!stack.isEmpty() && heights[stack.peek()] >= cur) { // find right bound
int h = heights[stack.pop()]; // one by one pop to use height to cal the area
int w = stack.isEmpty() ? i : i - stack.peek() - 1; // 2 - 0 - 1
ans = Math.max(ans, h * w);
} class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int ans = 0;
for (int i = 0; i <= heights.length; i++) {
int cur = (i == heights.length) ? -1 : heights[i];
while (!stack.isEmpty() && heights[stack.peek()] >= cur) { // find right bound
int h = heights[stack.pop()]; // one by one pop to use height to cal the area
int w = stack.isEmpty() ? i : i - stack.peek() - 1; // 2 - 0 - 1
ans = Math.max(ans, h * w);
}
stack.push(i);
}
return ans;
}
}int rightBound = i;
int leftBound = stack.isEmpty() ? -1 : stack.peek();class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<>();
// stack to record index,
// for i = 0 ~ heights.length
// while cur >= peek(), means find right bound = i,
// so pop h, get w,
// if stack is empty, means left bound is -1, or left bound is stack.peek()
// cal area
// push
int max = 0;
for (int i = 0; i <= heights.length; i++) {
int cur = (i == heights.length) ? -1 : heights[i];
while (!stack.isEmpty() && heights[stack.peek()] >= cur) {
int h = heights[stack.pop()];
int rightBound = i;
int leftBound = stack.isEmpty() ? -1 : stack.peek();
max = Math.max(max, h*(rightBound - leftBound - 1));
}
stack.push(i);
}
return max;
}
}class Solution {
public int largestRectangleArea(int[] heights) {
Deque<Integer> stack = new ArrayDeque<>();
int max = Integer.MIN_VALUE;
// 因為是要左邊界右邊界, 所以要執行到 height的尾巴 i <= heights.length
for (int i = 0; i <= heights.length; i++) { // why <= heights.length
int curr = (i == heights.length) ? -1 : heights[i];
while (!stack.isEmpty() && curr <= heights[stack.peek()]) {
int h = heights[stack.pop()];
int rightBound = i;
int leftBound = stack.isEmpty() ? -1 : stack.peek();
max = Math.max(max, h*(rightBound - leftBound - 1));
}
stack.push(i);
}
return max;
}
}