Ctrl K

Widest Fence Span Around the Marked Board

Hard
A fence is made of boards of heights heights[0], heights[1], …. The board at index anchor is marked. Choose a run of consecutive boards from i to j that contains the marked board (i ≤ anchor ≤ j). Its value is the height of its lowest board times the number of boards: min(heights[i..j]) · (j − i + 1). Return the largest value of such a run.
Example 1
heights=[2, 6, 5, 8, 1], anchor=2 → 15

Boards 1 through 3: the lowest is 5, there are three boards, 5 × 3 = 15. Adding the board of height 2 or 1 drops the height more than the width grows.

Example 2
heights=[3, 3, 3, 3], anchor=0 → 12

All boards are equal, so take the whole fence: 3 × 4 = 12.

Example 3
heights=[1, 9, 1], anchor=1 → 9

Board 9 alone gives 9, while with its neighbours it is only 1 × 3 = 3.

Constraints
  • 1 ≤ heights.length ≤ 10⁵
  • 1 ≤ heights[i] ≤ 2 · 10⁴
  • 0 ≤ anchor < heights.length
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCut the Middle to Restore Order MediumWater Between Walls HardLargest Number From Two Rows HardSecond Larger to the Right HardBest Pair Above the Bars Hard
loading Python…
Input
heights=[2, 6, 5, 8, 1], anchor=2
Expected
15

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