Beautiful Towers I - Problem
You are given an array heights of n integers representing the number of bricks in n consecutive towers. Your task is to remove some bricks to form a mountain-shaped tower arrangement.
In this arrangement, the tower heights are non-decreasing, reaching a maximum peak value with one or multiple consecutive towers, and then non-increasing.
Return the maximum possible sum of heights of a mountain-shaped tower arrangement.
Input & Output
Example 1 — Basic Mountain
$
Input:
heights = [5,3,4,1,1]
›
Output:
13
💡 Note:
Best mountain with peak at index 0: [5,3,3,1,1] with sum 13
Example 2 — Single Tower
$
Input:
heights = [6]
›
Output:
6
💡 Note:
Only one tower, so the mountain is just [6] with sum 6
Example 3 — Decreasing Array
$
Input:
heights = [5,4,3,2,1]
›
Output:
15
💡 Note:
Already mountain-shaped with peak at start: [5,4,3,2,1] sum = 15
Constraints
- 1 ≤ heights.length ≤ 103
- 1 ≤ heights[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code