225. Container With Most Water

Medium
Two Pointers
Array

Problem

Given an integer array height where height[i] represents the height of a vertical line at position i, choose two lines that together with the x-axis form a container. Return the maximum amount of water the container can hold.

Examples

Example 1

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

Example 2

Input: [1,1]
Output: 1

Example 3

Input: [4,3,2,1,4]
Output: 16
Constraints

2 <= height.length <= 100000

0 <= height[i] <= 10000

Hints

?? The area is determined by the shorter of the two selected lines.

?? Start with pointers at both ends.

?? Moving the taller line inward cannot improve the height limitation, so move the shorter line.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you prove why moving the shorter pointer is always safe?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,8,6,2,5,4,8,3,7]
Expected
49