Skip to content

Instantly share code, notes, and snippets.

@shz117
Created May 13, 2015 16:02
Show Gist options
  • Save shz117/828b007a6dcafa1e03c6 to your computer and use it in GitHub Desktop.
Save shz117/828b007a6dcafa1e03c6 to your computer and use it in GitHub Desktop.
LargestRectangleInHistogram
public class Solution {
public int largestRectangleArea(int[] height) {
Stack<Integer> st = new Stack<>();
int maxArea = 0;
for (int i = 0; i <= height.length; i++) {
int h = i == height.length ? 0 : height[i];
if (st.isEmpty() || h >= height[st.peek()])
st.push(i);
else {
int top = height[st.pop()];
maxArea = Math.max(maxArea, top * (st.isEmpty() ? i : i - 1 - st.peek()));
i--;
}
}
return maxArea;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment