i stands at position i and has height heights[i].
Pick two boards; together with the ground they form a tank. Water fills it up to the height of the shorter board, and the tank's width is the distance between the two boards. Any boards standing between them do not affect the volume.
Return the largest amount of water (height × width) that two boards can hold.heights=[3, 9, 2, 4, 7, 1] → 21
Take the boards at indices 1 and 4: the water level is limited by the shorter one (7) and they are 3 apart, so 7 · 3 = 21.
heights=[5, 0, 0, 5] → 15
The two outer boards of height 5 are 3 apart, giving 15. The zero-height boards in between do not get in the way.
heights=[6, 2] → 2
heights=[3, 9, 2, 4, 7, 1]
21
Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.