Google interview questions: Find the largest rectangle in the bar chart

Source: Internet
Author: User

Find the largest rectangle in the column chart: Give a group of non-negative integers to represent a column chart, design an algorithm to find the largest area of the rectangle that can fit into the column chart. For example, there are two possible solutions for the number of groups, 1 2 3 4 1, one is suitable for the rectangle within 2 3 4, and the area is 2*3; the other is a rectangle suitable for up to 3 4, with an area of 3*2. Do you think there are any O (n) algorithms?

This is a Google interview question. The following shows my solution. My setting is that the height of all bar charts is between 0 and 10.

 1 #include <iostream> 2 #include <vector> 3  4 using namespace std; 5  6 int MaxRectBarChart(vector<int> barChart) 7 { 8     int i_areaArray[11] = {}; 9     int i_tempareaArray[11] = {};10     vector<int>::iterator iter = barChart.begin();11     int max = 0;12     for (; iter != barChart.end(); iter++)13     {14         for (int i = 1; i <= *iter; i++)15         {16             i_tempareaArray[i] += i;17         }18         for (int i = *iter + 1; i < 11; i++)19         {20             if (i_tempareaArray[i] > i_areaArray[i])21                 i_areaArray[i] = i_tempareaArray[i];22             i_tempareaArray[i] = 0;23         }24     }25     for (int i = 1; i < 11; i++)26     {27         if (max < i_areaArray[i])28             max = i_areaArray[i];29     }30     return max;31 }32 33 int main()34 {35     vector<int> iv_highArray;36     int i_insert;37     while (cin >> i_insert)38     {39         iv_highArray.push_back(i_insert);40     }41     cout << "Max Rectangle area is " << MaxRectBarChart(iv_highArray) << endl;42     return 0;43 }

There is also a similar Google interview question that can be extended as the last question:

Find the largest White Rectangle in a single map: Give You a n * n black/white bitmap, find the largest white rectangle. Note that it is a rectangle, not any white area. Can your algorithm reach O (N * n?

 

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.