Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'Largest Rectangle in Histogram' and 'Container With Most Water'

Tags:

algorithm

http://www.leetcode.com/onlinejudge

I am not able to see the difference between these two questions. To me, these two questions are same, but they are NOT.

Can someone give me some hints that explain why they are different.

Thank you

like image 332
q0987 Avatar asked Sep 21 '12 04:09

q0987


People also ask

What is a rectangular histogram?

Each rectangle in the histogram represents a class of data. The width of the rectangle corresponds to the width of the class interval, and the surface of the rectangle represents the weight of the class.


2 Answers

The "container of water" solution will allow the water to rise above intermediate positions. With the "largest rectangle" problem, the rectangle cannot rise above intermediate bars.

like image 69
Ted Hopp Avatar answered Sep 25 '22 21:09

Ted Hopp


The "container of water" question is not as clearly described as the largest rectangle one, however I've been asked the water one in an interview.

The container of water one is basically asking for the area of the biggest "valley" in between the bars on a histogram. Looking at the histogram in the largest rectangle example, the answer would be "1" because there are two troughs made by the graph, a 1x1 trough on the left side, and a 1x1 trough on the right side. The max of these is of course, 1.

like image 20
mpuncel Avatar answered Sep 23 '22 21:09

mpuncel