[LeetCode][Java] Largest Rectangle in Histogram

來源:互聯網
上載者:User

標籤:leetcode   java   largest rectangle in   

題目:

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].


For example,
Given height = [2,1,5,6,2,3],
return 10.

題意:

給定n個非負的整數來表示長條圖塊的高度,每個直方塊的寬度都是1,在長條圖中找出面積最大的矩形。

比如(如所示):

給定高度height = [2,1,5,6,2,3],返回 10.

演算法分析:

參考部落格:http://pisxw.com/algorithm/Largest-Rectangle-in-Histogram.html

該題求最大面積的矩形,比較容易理解的思路,就是從每一個bar往兩邊走,以自己的高度為標準,直到兩邊低於自己的高度為止,然後用自己的高度乘以兩邊走的寬度得到矩陣面積。因為我們對於任意一個bar都計算了以自己為目標高度的最大矩陣,所以最好的結果一定會被取到。每次往兩邊走的複雜度是O(n),總共有n個bar,所以時間複雜度是O(n2)。

這邊在求一個bar左邊低於自己高度的最大x位置和右邊低於自己高度的最小x位置時,可以採用棧來求解。以求解左邊為例:如果棧頂的位置高度比bar的高度高,則不斷出棧,如果棧為空白,說明bar的左邊界能達到最左邊位置,則令其left=-1,否則棧頂的位置就是bar的左邊界,然後將bar壓入棧中,在求解下一個bar的左邊界,這樣遍曆一遍就知道每個bar的左邊界位置。

同理可以求解每個bar的右邊界位置。這樣每個bar能形成的最大矩形面積為height(bar)*(right-left-1),整個時間為O(n)+O(n)+O(n),分別為求左邊界,求右邊界,求最大面積,這樣總的時間複雜度為O(n)


AC代碼:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution {    public int largestRectangleArea(int[] height)     {        if(height==null || height.length==0)            return 0;        if(height.length==1)            return height[0];        //使用棧來求解每個bar左邊高度小於H(bar)的最大的x座標,記為left        int[] left=new int[height.length];        LinkedList<Integer> stack=new LinkedList<Integer>();        stack.push(0);        for(int i=0;i<height.length;i++)        {            while(stack.size()!=0 &&(height[stack.peek()]>=height[i]))            {                stack.pop();            }            if(stack.size()==0)            {                left[i]=-1;                stack.push(i);            }                    else            {                left[i]=stack.peek();                stack.push(i);            }            }        //同理使用棧來求解每個bar右邊高度小於H(bar)的最小的x座標,記為right        int[] right=new int[height.length];        LinkedList<Integer> stack2=new LinkedList<Integer>();        stack2.push(height.length-1);        for(int i=height.length-1;i>=0;i--)        {            while(stack2.size()!=0 &&(height[stack2.peek()]>=height[i]))            {                stack2.pop();            }            if(stack2.size()==0)            {                right[i]=height.length;                stack2.push(i);            }                        else            {                right[i]=stack2.peek();                stack2.push(i);            }            }        //計算每個bar能形成的矩形的面積,並求得一個最大面積        int maxRec=0;        for(int i=0;i<height.length;i++)        {            int rec=(right[i]-left[i]-1)*height[i];            maxRec=Math.max(maxRec,rec);        }        return maxRec;    }}</span>


著作權聲明:本文為博主原創文章,轉載註明出處

[LeetCode][Java] Largest Rectangle in Histogram

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.