Imagine you're an architect tasked with designing a stunning skyline along a coordinate line. You have n positions where you must build towers, and each position i has a maximum height constraint maxHeights[i].
Your goal is to create a beautiful mountain-shaped configuration that maximizes the total height sum. A configuration is considered beautiful when:
- Each tower's height is between
1andmaxHeights[i] - The heights form a mountain array - they increase up to a peak, then decrease
Mountain Array Definition: There exists a peak index i where heights are non-decreasing from left to peak (heights[j-1] ≤ heights[j] for all j ≤ i) and non-increasing from peak to right (heights[k] ≥ heights[k+1] for all k ≥ i).
Return the maximum possible sum of heights for a beautiful tower configuration.
Input & Output
Visualization
Time & Space Complexity
Each element is pushed and popped from stack at most once, two passes through array
Space for monotonic stacks and left/right contribution arrays
Constraints
- 1 ≤ maxHeights.length ≤ 105
- 1 ≤ maxHeights[i] ≤ 109
- The answer is guaranteed to fit in a 64-bit integer