Loading...
Loading...
A hostel courtyard has n concrete walls in a row, one metre apart. Wall i has height h[i] metres. During the monsoon, the students want to block water between two walls to make a small pool. The water level cannot go above the shorter of the two walls. The amount of water is the distance between the two walls times that level. Print the largest amount of water any two walls can hold.
Example
Input
9 1 8 6 2 5 4 8 3 7
Output
49
Walls 2 and 9 have heights 8 and 7. They are 7 apart, so they hold 7 × 7 = 49.
Try the problem first. Open one hint at a time.
Start with the widest pair: the first wall and the last wall.
The water level is set by the shorter wall. Moving the taller wall inward can only make the width smaller without raising the level.
So move the shorter wall inward, compute the new amount, and keep the best one seen.
Stop when the two indexes meet. Use long for the amount.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor