Gamified challenge · Two pointers
heights[i] is the height of tower i on the quay, and towers stand one unit apart. The crane stretches a tank between two towers i < j: it is j - i wide and fills only up to the shorter of the two towers, so it holds (j - i) × min(heights[i], heights[j]). The towers in between are thin poles that don't get in the way.
Write maxArea(heights) (max_area in Python) that returns the most water any one tank can hold. With fewer than two towers no tank fits, so return 0.
For [1, 8, 6, 2, 5, 4, 8, 3, 7] the best tank hangs between towers 1 and 8: width 7 × height 7 = 49.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google