zoukankan      html  css  js  c++  java
  • leetcode--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 height = [2,1,5,6,2,3],
    return 10.

    Have you been asked this question in an interview? 

    method 1: 

    public class Solution {
       /**
    	 * The algorithm in this problem is similar the one in problem of trapping rain water problem.
    	 * We use two stacks to save some indices of the starting position and ending position of elements.<br>
    	 * The running time of this algorithm is O(n).
    	 * @param height -int array which denotes the heights
    	 * @return int -maximum rectangle area
    	 * @author Averill Zheng
    	 * @version 2014-06-22
    	 * @since JDK 1.7
    	 */
    	public int largestRectangleArea(int[] height) {
    		int max = 0, length = height.length;
    	    Stack<Integer> indices = new Stack<Integer>(); 
    	    Stack<Integer> leftEnd = new Stack<Integer>();
    	    indices.push(0);
    	    leftEnd.push(0);
    	    if(length > 0){
    	    	max = height[0];
    	        for(int i = 1; i < length; ++i){
    	        	if(height[indices.peek()] <= height[i]) {
    	        		max = Math.max(max, height[i]);
    	        		leftEnd.push(i);
    	        		indices.push(i);
    	        	}
    	        	else {
    	        		int left = 0, start = indices.peek();
    	        		while(!indices.empty() && height[indices.peek()] >= height[i]){
    	        			left = leftEnd.pop();
    	        			max = Math.max(max, height[indices.pop()] * (start - left + 1));
    	        		}
    	        		leftEnd.push(left);
    	        		indices.push(i);
    	        	}
    	       	}
    	        while(!indices.empty()){
    	        	max = Math.max(max, height[indices.pop()] * (length - leftEnd.pop()));
    	        }
    	    }
    	    return max;
        }
    }
    

    method 2: The algorithm to this problem is a little bit trick. 

    The running time of this algorithm is O(n) where n is the length of height.

    1. scan the height, if the array is not increasing, then remove the previous bigger elements.(in this step, we need to compare the area with the current maximum area)

    2. after the step 1, then the remaining elements of the array is increasing. then calculate the maximum.

    public class Solution {
        public int largestRectangleArea(int[] height) {
            int area = 0;
            int len = height.length, curr = 0;
            Stack<Integer> left = new Stack<Integer>();
            Stack<Integer> index = new Stack<Integer>();
            while(curr < len){
                if(curr == 0 || height[curr] >= height[index.peek()]){
                    left.push(curr);
                    index.push(curr);
                }
                else if(height[curr] < height[index.peek()]){
                        int last;
                        //in this following while loop, it means that we remove the
                        //elements which is greater then height[curr].
                        do{
                            last = left.pop();
                            area = Math.max(area, height[index.pop()] *(curr - last));
                        }while(!left.isEmpty() && height[curr] < height[index.peek()]);
                        
                        //we need to record the position of height[curr] when we removed the 
                        //previous larger elements
                        left.push(last);
                        //we also need to record the value of the height[curr]
                        index.push(curr);
                    }
                    curr++;
                }
                while(!index.isEmpty() && !left.isEmpty())
                    area = Math.max(area, height[index.pop()] * (len - left.pop()));
                
            return area;
        }
    }
    

      

  • 相关阅读:
    Mysql int类型你了解多少
    java 小程序开发PKCS7Padding 解密相关问题
    Shiro+JWT 实现权限管理(二)--JWT
    Shiro+JWT 实现权限管理(一)--Shiro
    HTTP常见状态码
    Java开发之Redis
    微信公众号开发总结(一) --程序入口
    成熟男人需要懂得的100件事
    Java8 Time API与老Date之间的转换
    极光推送工具类
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3610493.html
Copyright © 2011-2022 走看看