首页 > 编程语言 > 详细

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

时间:2015-05-19 20:40:38      阅读:242      评论:0      收藏:0      [点我收藏+]

For example,
Given height = [2,1,5,6,2,3],
return 10.

解题思路:

参考Problem H: Largest Rectangle in a Histogram 第四种思路,或者翻译版Largest Rectangle in Histogram@LeetCode,JAVA实现如下:

    public int largestRectangleArea(int[] height) {
        Stack<Integer> stk = new Stack<Integer>();
        int ret = 0;
        for (int i = 0; i <= height.length; i++) {
        	int h=0;
        	if(i<height.length)
        		h=height[i];
            if (stk.isEmpty() || h >= height[stk.peek()])
                stk.push(i);
            else {
            	int top = stk.pop();
                ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
                i--;  
            }
        }
        return ret;
}

 

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

原文:http://www.cnblogs.com/tonyluis/p/4515219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!