Ctrl K

Snuffing Lanterns for Points

Hard
A row of lanterns hangs along an alley, each with its own brightness lights[i]. A keeper puts them out one at a time, in any order he likes, until all are dark. Putting out a lantern earns points: its brightness times the brightness of the nearest still-lit lantern on its left and the nearest still-lit lantern on its right. If no lit lantern remains on one side, that side counts as 1. A lantern that has been put out drops out of the row, so its former neighbours become adjacent. Return the largest total score the keeper can collect.
Example 1
lights=[2, 4, 3] → 33

The best order is 4 first (2·4·3 = 24), then 2 (1·2·3 = 6), then 3 (1·3·1 = 3): 33 in total.

Example 2
lights=[6] → 6

A single lantern has no neighbours, so both sides count as 1: 1·6·1 = 6.

Example 3
lights=[3, 0, 5] → 20
Constraints
  • 1 ≤ len(lights) ≤ 300
  • 0 ≤ lights[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
lights=[2, 4, 3]
Expected
33

Run checks the first 3 tests in your browser. Submit checks all 93 tests on the server, hidden ones included.