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

      

  • 相关阅读:
    sqlserver查询当前库下,一张表的表名,字段名,字段类型,字段长度
    JAVA将汉字转换为全拼以及返回中文的首字母,将字符串转移为ASCII码
    Springmvc 简单入门1
    第一篇,java学习之旅
    基于html5,父级块中添加video,不能全屏播放的问题解决。
    windows10 jdk8下载与安装
    R 安装 Bioconductor 的网络连接问题
    R 内存管理问题
    MCMC sampling 【转】
    最大似然估计(Maximum likelihood estimation)【转】
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3610493.html
Copyright © 2020-2023  润新知