My accepted java solution


  • 0
    B

    public static int largestrectangle(int[] height){
    int maxArea = 0;
    Stack<Integer> s = new Stack<Integer>();
    int len = height.length;
    int i = 0;
    while(i<=len){
    int h = (i==len? 0:height[i]);
    if(s.isEmpty()||h>=height[s.peek()]){
    s.push(i);
    i++;
    }
    else{
    int tp = s.pop();
    maxArea = Math.max(maxArea, height[tp]*(s.isEmpty()? i: i-1-s.peek()));
    }
    }
    return maxArea;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.