[leetcode]84 largest Rectangle in histogram

Source: Internet
Author: User

https://oj.leetcode.com/problems/largest-rectangle-in-histogram/

http://blog.csdn.net/linhuanmars/article/details/20524507

Public class solution {    public int largestrectanglearea (int[]  height)      {        // solution  a        // return largestrectanglearea_expand (height) ;                //  Solution b        return largestrectanglearea_stack (height);     }        ////////////////////     // Solution A: Stack    //    private  Int largestrectanglearea_stack (int[] h)     {         if  (h == null | |  h.length == 0)             return 0;   // invalid input                     Stack<Integer>  stack = new Stack<Integer> (        int);  max = 0;        for  (int i = 0  ; i < h.length ; i ++)         {             while  (!stack.empty ()  & & h[i] <= h[stack.peek ()])              {                 Int last = stack.pop ();                 int area = 0;                 if  (Stack.empty ())                  {                     area = i * h[last];                 }                 else                 {                     //  next   stack.peek ()  + from the top element of the current stack  1 to                      //  current subscript  i  (exclusive)                      //  is larger than the stack element height                      area =  (i -  ( Stack.peek ()  + 1)  * h[last];                 }                 max = math.max (Max, area);             }                         stack.push (i);         }                 while  (!stack.empTy ())         {             int last = stack.pop ();             int area = 0;             if  (Stack.empty ())             {                 area =  h.length * h[last];            }             else             {                 area =  (H.length - stack.peek ()  - 1)  * h [LAST];&NBSP;&NBSP;&NBSP;&NBsp;        }             max = math.max (Max, area);         }                 return  max;    }        ////////////////////     // Solution B: Expand    //    //  For each bar i,  search   leftmost  >= h[i]   index    //  same   Find the right      //  calculate the maximum     //     //  o (n ^ 2)     private int largestrectanglearea_expand (int[] h )     {        int max = 0;         for  (int i = 0 ; i < h.length ; i ++)         {             // find left            int  l = i;            while  (l  >= 0 && h[l] >= h[i])                  l --;             l++;                         // find right             int r = i;             while  (R < h.length && h[r] >= h[i])                  r ++;             r --;                          max = math.max (max, h[i] *  (r - l + 1));         }        return max;     }}


[leetcode]84 largest Rectangle in histogram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.