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;
        }
    }
    

      

  • 相关阅读:
    Ubuntu18.04可执行文件运行提示No such file or directory
    Linux gcc(ar命令)打包库到另一个库中的另外一种方法
    干干净净的grep
    C语言里面和时间有关的函数
    Ubuntu上安装tftp服务
    因为新冠,宅家2周,折腾了一下电视盒子。
    批量处理文件的Python程序
    Windows 10中使用VirtualBox
    重采样Resample 的一些研究记录。
    微信公众号接入第三方服务器,设置自动回复、关键回复、自定义菜单,配置及开发流程
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3610493.html
Copyright © 2011-2022 走看看