Leetcode Analysis _84. Largest Rectangle in histogram

Source: Internet
Author: User
Tags compact int size
"title"

Topic link
Given n non-negative integers representing the histogram ' s bar height where the width of each bar are 1, find the area of L Argest rectangle in the histogram.


Above is a histogram where width to each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown into the shaded area and which has area = ten unit.

For example,
Given Heights = [2,1,5,6,2,3],
return "Analysis"

Best Runtime:

Just see this problem when you know you can use dynamic programming, but due to limited capacity, so to the end or did not write an O (n), can only help and other bloggers online, only to find the idea is: Wait until heights[i]<=heights[i-1] The time to calculate, ask Max_height
And there is the heights of a number 0 after the vector, which loops

Class Solution {public
:
    int Largestrectanglearea (vector<int>& heights) {
        heights.push_back (0 );
        int size = Heights.size ();
        stack<int> s;
        int max_area = 0;
        for (int i = 0; i < size i + +) {while
            (!s.empty () && heights[s.top ()] > Heights[i]) {
                int top = S.top ();
                S.pop ();
                Max_area = Max (Max_area, Heights[top] * (I-(S.empty ()? -1:s.top ())-1);
            S.push (i);
        }
        return max_area;
    }
;

Of course, our stacks pop operation can be rewritten using I-compact to avoid always going back and forth pop operations , like this:

Class Solution {public
:
    int Largestrectanglearea (vector<int>& heights) {
        heights.push_back (0 );
        int size = Heights.size ();
        stack<int> s;
        int max_area = 0;
        for (int i = 0; i < size; I + +) {
            if (s.empty () | | heights[s.top ()] <= heights[i]) s.push (i);
            else{
                int top = S.top ();
                S.pop ();
                Max_area = Max (Max_area, Heights[top] * (S.empty () i:i-1-s.top ()));
                I--; 
            }
        }
        return max_area;
    }
;

This time with the if-else structure rather than the while structure, because I-compact, and then in the For loop will also i++ so with the above method of thinking is the same

But the efficiency of the above two solutions has always been between 22ms and 26ms , the runtime on the Leetcode is only 50% , because we always need to push the operation, Each time you need to start a space to push
So we can use vector or array structure, use i++,i– to operate, improve efficiency

Class Solution {public
:
    int Largestrectanglearea (vector<int>& heights) {
        heights.push_back (0 );
        int cur = 0, Max_area = 0, top = 0;
        int * stack = new int[heights.size ()];
        Vector<int> Stack (heights.size (), 0);
        Stack[top] =-1;
        for (int i = 0; i < heights.size (); ++i) {while
            (top>0 && heights[stack[top] >= heights[i]) {
                cur = (i-stack[top-1]-1) *heights[stack[top]];
                top--;
                Max_area = max (cur, max_area);
            }
            Stack[++top] = i;
        }
        return max_area;
    }
;

Efficiency as shown:

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.