Skip to content

Practice Beautiful Towers II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2866
LeetCode Problem

Beautiful Towers II

Problem Statement

Choose positive integer heights with heights[i] <= maxHeights[i]. They must form a mountain: for some peak position, heights are nondecreasing up to the peak and nonincreasing afterward. Equal neighboring heights and a peak at either end are allowed. Return the greatest possible sum of heights.

Source: Beautiful Towers II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
maxHeights = [5,3,4,1,1]
Output
13

# Example 2

Input
maxHeights = [6,5,3,9,2,7]
Output
22

# Example 3

Input
maxHeights = [3,2,5,5,2,3]
Output
18

Constraints

1 <= n == maxHeights.length <= 10^5
1 <= maxHeights[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Beautiful Towers II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.