Quant Memo
Coding/●●●●●

Container with most water

Asked at DE Shaw

You're given n vertical lines; line i has height height[i] and stands at x-coordinate i. Choose two lines that, together with the x-axis, contain the most water. The water level is set by the shorter line, so the area between lines i and j is (ji)min(hi,hj)(j - i)\cdot\min(h_i, h_j).

height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
-> 49          # lines at indices 1 and 8: (8 - 1) * min(8, 7)

Return the maximum area. Aim for O(n)O(n), and be ready to justify why your pointer move is safe.

Show a hint

Start with the widest container. If you move a pointer inward you lose width, which pointer could possibly be worth moving?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions