Skip to content

Practice Beautiful Towers I

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

Medium#2865
LeetCode Problem

Beautiful Towers I

Problem Statement

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.

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

Examples

# Example 1

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

# Example 2

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

# Example 3

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

Constraints

1 <= n == heights.length <= 10^3
1 <= heights[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Beautiful Towers I.

Review your practice with PhantomCodeAI

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