84. Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given heights = [2,1,5,6,2,3],

return 10.

Another exammple

histogram

The O(n2) solution:

http://www.geeksforgeeks.org/largest-rectangle-under-histogram/

For each bar extending it as far as possible, time complexity O(n2);

The O(nlgn) solution:

http://www.geeksforgeeks.org/largest-rectangle-under-histogram/

We define a method findMax(int start, int end, int[] h)

first we find the minValue of the h, h[k],

we pick up the max from the findMax(start,k-1,h),  findMax(k+1,end,h) and (end - start + 1) * h[k]      as the result.

how to find the minValue ? Scan in O(n) , OR Segment Tree in O(lgn).


The O(n) solution:

maintain a Monotone stack, increasing:

do the following step:

1) Create an empty stack.

2) Start from first bar, and do following for every bar ‘hist[i]’ where ‘i’ varies from 0 to n-1.
……a) If stack is empty or hist[i] is higher than the bar at top of stack, then push ‘i’ to stack.
……b) If this bar is smaller than the top of stack, then keep removing the top of stack while top of the stack is greater. Let the removed bar be hist[tp]. Calculate area of rectangle with hist[tp] as smallest bar. For hist[tp], the ‘left index’ is previous (previous to tp) item in stack and ‘right index’ is ‘i’ (current index).

3) If the stack is not empty, then one by one remove all bars from stack and do step 2.b for every removed bar.

Code:

public class Solution {
    public int largestRectangleArea(int[] h) {
        int ret = 0;
        Stack<Integer> s = new Stack<>();
        for(int i = 0 ; i < h.length; i++){
            if(s.empty() || h[s.peek()] <= h[i]){
                s.push(i);
            } else {
                while(!s.empty() && h[s.peek()] >= h[i]){
                    int index = s.pop();
                    
                    int left = s.empty() ? -1 : s.peek();
                    ret = Math.max(ret,h[index] * (i - 1 - left)); // (i - 1)  right index included, left  left index excluded, 
                }
                s.push(i);
            }
        }
        
        while(!s.empty()){
            int index = s.pop();
            int left = s.empty() ? -1 : s.peek();
            ret = Math.max(ret,h[index] * (h.length - 1 - left)); // h.length - 1 right index included, left  left index excluded 

        }
        return ret;
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值