Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Container With Most Water

Medium
two-pointersarraygreedy

You are given an array height where the i-th element is the height of a vertical line at position i. Choose two lines that, together with the x-axis, form a container holding the most water. Return the maximum amount of water.

Input: one line of space-separated integers height. Output: the maximum area.

Example 1
Input:
1 8 6 2 5 4 8 3 7
Output:
49

Lines at indices 1 and 8: min(8,7) × 7 = 49.

Example 2
Input:
1 1
Output:
1

Constraints

  • 2 ≤ height.length ≤ 10^5
  • 0 ≤ height[i] ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.