Container With Most Water

Medium Solved

Description

Given n non-negative integers height where each represents a vertical line at coordinate i. Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water (area).

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]

Output: 49

Example 2:

Input: height = [1,1]

Output: 1

Example 3:

Input: height = [4,3,2,1,4]

Output: 16

Note:

Runner expects a single line stdin with a JSON array of integers (e.g. [1,8,6,2,5,4,8,3,7]) and prints the integer maximum area.

Your Submissions

No submissions yet.

Discuss

Share two-pointer greedy solution, complexity analysis, and corner cases.

Test Cases

Test Case 1
Test Case 2
Test Case 3