⌘K
Flycodx

Container With Most Water

Container With Most Water

MediumArrays75% accepted

You are given n non-negative integers representing heights of vertical lines at positions 1 through n. Find two lines that together with the x-axis form a container that holds the most water. The amount of water a container holds = min(height[left], height[right]) × (right - left). Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: n=9, heights=[1 8 6 2 5 4 8 3 7] → 49

Input format — only if your code reads from stdin

Input format (stdin): - First line: integer n (number of heights) - Second line: n space-separated non-negative integers Output: a single integer — the maximum water the container can hold.

Approach

Two-pointer greedy: start with left=0 and right=n-1. Compute current area = min(h[left], h[right]) × (right-left), update max. Move the pointer with the smaller height inward — this is the only way to potentially increase area.

Input

9 1 8 6 2 5 4 8 3 7

Output

49
  • •2 ≤ n ≤ 10^5, 0 ≤ height[i] ≤ 10^4
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3