educative.io

Output for example 1 seems to be wrong

As per question, Input: [1, 3, 5, 4, 1]
and answer is give as 6, which is between 3-4.

But from the graph, between 3-5, 3 levels of water can be stored and between 5-4, 4 levels of water can be stored, so the answer should be 7. Am I missing something here?

Also question needs to be rephrased because water level between first and last building will always be the maximum. Again am I missing something here too!

Hi,

Please find my answer in other discussion

We need to find two buildings that will trap the most water. As we can understand that the water will be trapped based on the height of the shorter building. While calculating this we won’t be considering the middle buildings. You can easily see if we consider all the middle buildings too, and try to calculate the trapped water between every adjacent building, that will be a different question where we are calculating the total water trapped among all buildings.