Container With Most Water
Medium
Arrays
Two Pointers
Greedy
Given n non-negative integers `heights` where each represents a point at coordinate (i, heights[i]), find two lines that together with the x-axis forms a container that holds the most water.
Input: space-separated integers
Output: maximum area
Constraints
n == heights.length
2 <= n <= 100000
0 <= heights[i] <= 10000
Examples
Example 1:
Input: 1 8 6 2 5 4 8 3 7
Output:
Example 2:
Input: 1 1
Output:
Hints
Use two pointers from both ends.
Move the shorter pointer inward.
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console