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

      

  • 相关阅读:
    [PHP]算法-归并排序的PHP实现
    [PHP] 数据结构-二叉树的创建PHP实现
    [PHP] 数据结构-循环链表的PHP实现
    [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现
    [PHP] 算法-两个n位的二进制整数相加问题PHP实现
    [PHP] 数据结构-线性表的顺序存储结构PHP实现
    [日常] 链表-头结点和头指针的区别
    [日常] C语言中指针变量
    [日常] 算法-单链表的创建-尾插法
    [日常] 算法-单链表的创建
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3610493.html
Copyright © 2011-2022 走看看