Loading
Loading
A row of vertical posts stands on flat ground, each with a given height. Choosing two posts and stretching a sheet between them forms a container; the water it holds is limited by the shorter post and by the distance between them.
Return the largest volume any pair of posts can hold.
heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]left=0 (1), right=8 (7)min 1 × width 8 = 8; move the shorterleft=1 (8), right=8 (7)min 7 × width 7 = 49…no wider pair beats it49Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Posts at indices 1 and 8: min(8,7) × 7 = 49.
Example 2