Created
May 13, 2015 16:02
-
-
Save shz117/828b007a6dcafa1e03c6 to your computer and use it in GitHub Desktop.
LargestRectangleInHistogram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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