Easy understand java solution


  • 0
    D
    public int largestRectangleArea(int[] heights) {
    		
    		Stack<Integer> stack = new Stack<>();
    		
    		int max = 0;
    		for (int i = 0; i <= heights.length; i++){
    			int curr = (i == heights.length) ? 0: heights[i];
    			int count = 0;
    			while(!stack.isEmpty() && heights[stack.peek()] > curr){
    				int pos = stack.pop();
    				max = Math.max(max, heights[pos] * (i - pos));
    				heights[pos] = curr;
    				count++;
    			}
    			i -= count;
    			stack.push(i);
    		}
    		
            return max; 
        }
    

Log in to reply
 

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