2865. Beautiful Towers I Medium

@problem@discussion
#Array#Stack#Monotonic Stack



1/**
2 * [2865] Beautiful Towers I
3 *
4 * 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.
5 * Return the maximum possible sum of heights of a mountain-shaped tower arrangement.
6 *  
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">heights = [5,3,4,1,1]</span>
10 * Output: <span class="example-io">13</span>
11 * Explanation:
12 * We remove some bricks to make heights = [5,3,3,1,1], the peak is at index 0.
13 * </div>
14 * <strong class="example">Example 2:
15 * <div class="example-block">
16 * Input: <span class="example-io">heights = [6,5,3,9,2,7]</span>
17 * Output: <span class="example-io">22</span>
18 * Explanation:
19 * We remove some bricks to make heights = [3,3,3,9,2,2], the peak is at index 3.
20 * </div>
21 * <strong class="example">Example 3:
22 * <div class="example-block">
23 * Input: <span class="example-io">heights = [3,2,5,5,2,3]</span>
24 * Output: <span class="example-io">18</span>
25 * Explanation:
26 * We remove some bricks to make heights = [2,2,5,5,2,2], the peak is at index 2 or 3.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	1 <= n == heights.length <= 10^3
32 * 	1 <= heights[i] <= 10^9
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/beautiful-towers-i/
38// discuss: https://leetcode.com/problems/beautiful-towers-i/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn maximum_sum_of_heights(heights: Vec<i32>) -> i64 {
44        
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_2865() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.