Show problems as
Classic problem #11MediumArrayTwo PointersGreedy

Container With Most Water

Real-world version: Banner Between Pillars
Tracked in this browser only
Read Container With Most Water on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A stone walkway is lined with pillars, one pillar every metre. pillars[i] is the height in metres of the pillar at metre mark i. A stump left over from an old pillar has height 0.

A crew hangs one rectangular banner across the walkway. They pick two pillars, run a level rope between them and let the fabric hang from the rope all the way down to the paving. Because the rope has to sit at the same height on both pillars, it can be tied no higher than the lower of the two pillar tops, and the banner's height equals the rope's height. The banner's width is the gap in metres between the two chosen pillars.

Return the largest area, in square metres, that a single banner can cover. The rope is never tied at a tilt, and pillars standing between the chosen two are ignored: the fabric hangs freely past them.

Examples

Example 1

Input
pillars = [2, 9, 8, 1]
Output
8

Tying to marks 1 and 2 gives a rope at height 8 across a gap of 1 metre. Reaching for the widest gap, marks 0 and 3, only allows height 1 over 3 metres, which is 3 square metres.

Example 2

Input
pillars = [3, 1, 1, 1, 1, 3]
Output
15

The two end pillars are both 3 metres tall and stand 5 metres apart, so the banner covers 15 square metres. The short pillars in between do not get in the way.

Example 3

Input
pillars = [4, 7]
Output
4

With only one pair available, the rope sits at 4 metres across a 1 metre gap.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def largest_banner(pillars: list[int]) -> int:
Java
public int largestBanner(int[] pillars)