Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Max Rectangle In A Histogram (Main Thread)

Here is the interview question prompt, presented for reference.

We're given a histogram like the following, where contiguous (sharing a common border or touching) bars are made up of different heights. Let's assume all bars have the same width.

The histogram is converted to an array of the heights of the bars:

const histArr = [3, 1, 4, 2, 2, 1]

With this knowledge, can we find the largest rectangular area within the histogram? For the above, it would be the area outlined below:

With the above histogram, calling maxRectInHist(histArr) would get us 6. Can you fill in the method?

Constraints

  • Length of the array <= 100000
  • The array can be empty
  • The array will contain values between 1 and 10000
  • The final answer will always fit in the integer range
  • Expected time complexity : O(n)
  • Expected space complexity : O(n)

You can see the full challenge with visuals at this link.

Challenges • Asked over 3 years ago by Will Bowers

Jake from AlgoDaily Commented on Aug 30, 2020:

This is the main discussion thread generated for Max Rectangle In A Histogram.

Will Bowers Commented on Aug 30, 2020:

The third test case is wrong. The hist [1, 2, 3, 4] has a rect of size 6. 4 is not the max.

Stu Commented on Apr 12, 2021:

+1 to Will's comment - maxRectArea is 6 for [1, 2, 3, 4] either horizontally using indices (1, 2, 3) or vertically using indices (2, 3)

louis19930910 Commented on Apr 20, 2021:

Final solution is wrong.

In line 7, code should be
python
while index &lt; len(histArr):

Rahat Zaman Commented on Apr 26, 2021:

@Will, @Stu, @louis19930910,

Thank you for your support. Now the code along with the test cases are fixed for all languages. Happy Coding!