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: [3,3,3,1,1] with sum 11, or peak at index 2: [3,3,4,1,1] with sum 12, or peak at index 0: [5,3,1,1,1] with sum 11. Actually optimal is [5,3,4,1,1] → [3,3,4,1,1] = 12.
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
Understanding the Visualization
1
Input
Array of tower heights [5,3,4,1,1]
2
Process
Try each position as peak, shape left (non-decreasing) and right (non-increasing)
3
Output
Maximum sum of mountain-shaped arrangement
Key Takeaway
🎯 Key Insight: Try each position as peak and calculate maximum mountain sum by shaping both sides
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code