Applied AI Engineer (LLM) coding learning path Level 2 This level · 26 of 31

Container With Most Water

Medium Two pointers

Problem

You are given the heights of vertical lines drawn at positions 0 to n−1. Choose two lines so that together with the x-axis they form a container holding as much water as possible, and return that maximum area.

Original: LeetCode 11: Container With Most Water.

Examples

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 = [1, 2, 1]`
Output`2`

Constraints

  • n == height.length
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 11: Container With Most Waterleetcode.com
  2. Java SE 17 API: Math.mindocs.oracle.com
Previous
Next