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

      

  • 相关阅读:
    全世界最好听的钢琴曲
    清华“70后”院长刘云浩——生命在于运动,梦想从未止步 | 新“清”年特辑
    Android---60---Notification 通知栏的简单使用
    面向对象的三大特性
    一道淘汰85%面试者的百度开发人员面试题?
    Linux下基于Erlang的高并发TCP连接压力实验
    2014南瑞暑期实习面试笔试经历
    Oracle cloud control 12c 怎样改动sysmanpassword
    JEECG社区 一个微信教育站点案例源代码分享
    stm32智能小车之路之小车启动
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3610493.html
Copyright © 2011-2022 走看看