Advertisement

Beautiful Towers I - LeetCode 2865 Solution

Beautiful Towers I - Complete Solution Guide

Beautiful Towers I is LeetCode problem 2865, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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. Example 1: Input: heights = [5,3,4,1,1] Output: 13 Explanation: We remove some

Detailed Explanation

The problem asks us to find the maximum possible sum of heights of a 'mountain-shaped' tower arrangement that can be formed from a given array `heights`. A mountain-shaped arrangement is defined as a sequence of heights that are non-decreasing until a peak (maximum value) and then non-increasing after the peak. We need to remove bricks from the original towers (represented by the `heights` array) to achieve this mountain shape. The goal is to find the mountain shape that maximizes the sum of the tower heights.

Solution Approach

The solution uses a brute-force approach. It iterates through each element of the `heights` array, considering each element as a potential peak of the mountain. For each potential peak, it constructs the left and right slopes by minimizing the heights of the towers to satisfy the mountain shape property (non-decreasing to the left and non-increasing to the right). It calculates the sum of heights for the constructed mountain and updates the maximum sum found so far. This process is repeated for all possible peaks, and the maximum sum found across all peaks is the final result.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_sum` to 0. This variable will store the maximum sum of heights found so far.
  2. Step 2: Iterate through the `heights` array using a loop (from index `i = 0` to `n-1`), considering each element `heights[i]` as a potential peak.
  3. Step 3: For each potential peak `heights[i]`, initialize `current_sum` to `heights[i]`. This will accumulate the sum of heights for the current mountain shape.
  4. Step 4: Construct the left slope (non-decreasing). Start from `heights[i-1]` and move towards the beginning of the array. In each step, take the minimum between the previous tower's height (`last_h`, initially `heights[i]`) and the current tower's height (`heights[j]`). Update `last_h` with this minimum and add it to `current_sum`.
  5. Step 5: Construct the right slope (non-increasing). Start from `heights[i+1]` and move towards the end of the array. Similar to the left slope, take the minimum between the previous tower's height (`last_h`, initially `heights[i]`) and the current tower's height (`heights[j]`). Update `last_h` with this minimum and add it to `current_sum`.
  6. Step 6: After constructing both slopes, update `max_sum` with the maximum between `max_sum` and `current_sum`.
  7. Step 7: After iterating through all potential peaks, return `max_sum`.

Key Insights

  • Insight 1: The core idea is to iterate through each element of the input array `heights` and consider it as a potential peak of the mountain.
  • Insight 2: For each potential peak, we need to construct the left and right slopes of the mountain by ensuring non-decreasing and non-increasing properties, respectively. This involves reducing the height of towers where necessary.
  • Insight 3: Since the constraint n <= 1000, a brute-force approach iterating through each possible peak and calculating the sum around it is feasible within the time limit.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: Array, Stack, Monotonic Stack.

Companies

Asked at: Salesforce.