Find the largest rectangle in the bar chart & find the largest rectangle area in the matrix that contains only the same value

Source: Internet
Author: User

I. Specific questions: Give a set of non-negative integers to represent a bar chart. design an algorithm to obtain the area of the largest rectangle in the bar chart. For example, enter the following data:, and. Each number indicates the height of a column. If the width of a column is 1 by default, the area of the maximum rectangle is 10.


This problem exists in leetcode: largest rectangle in Histogram

Algorithm time complexity O (n ):

Int largestrectarea (vector <int> & height) {stack <int> P; int I = 0, Res = 0; height. push_back (0); // function: Clear the stack while (I 

2. Find the largest White Rectangle in a single map: Give You A nxn black/white bitmap to find the largest White Rectangle. Note that it is a rectangle, not any white area.

If we use the largest sub-array and the solution, the complexity is O (n ^ 3). In fact, we can use the above algorithm to solve the problem:

Chen liren: This is actually a variant of the above question "finding the largest rectangle from the bar chart. Basic Idea: scan the number of white points in each column on the row in the unit of behavior, and record them into an array similar to a bar chart. Consider the previous results, this can be O (N). Then, use the solution O (n) of the question to obtain the largest rectangle. Therefore, the complexity of the entire algorithm is O (n * n ). It is important for children to learn what they have learned.

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