All problems
0021MediumArrayTwo PointersGreedy

Banner Between Pillars

Tracked in this browser only
Write code

Trains the technique from

LeetCode 11Container With Most Water

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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