Skip to the content

The sail between two masts

Problem

"Rig it between any two masts you like," says the captain, handing you the spare sail. The sail is a rectangle: its width is the distance between the positions of the two masts, and its height is that of the shorter one, since there is nothing higher up to tie it to. Masts in between do not get in the way.

Write a function that takes masts, the list of heights in deck order, one per position (integers from 0 up; it may be empty). Return an integer: the area of the biggest sail you can rig. With fewer than two masts there is no sail: return 0.

With [3, 7, 2, 6], between the 7 (position 1) and the 6 (position 3) you get width 2 and height 6, area 12. Between the 3 and the 6: width 3, height 3, area 9. No other pair does better, so the answer is 12.

Examples

  • The example

    [3, 7, 2, 6] → 12

  • Nine masts

    [1, 8, 6, 2, 5, 4, 8, 3, 7] → 49

  • Just two masts

    [4, 9] → 4

  • The ends win

    [5, 1, 1, 1, 5] → 20

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def biggest_sail(masts):
    pass

JavaScript

function biggestSail(masts) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →