Beautiful Towers II - Complete Solution Guide
Beautiful Towers II is LeetCode problem 2866, 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 a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The i th tower is built at coordinate i and has a height of heights[i] . A configuration of towers is beautiful if the following conditions hold: 1 <= heights[i] <= maxHeights[i] heights is a mountain array. Array heights is a mountain if there exists an index i such that: For all 0 < j <= i , heights[j - 1] <= heights[j] For all i <= k < n - 1 , heights[k + 1] <= heights[k] Re
Detailed Explanation
The problem asks us to find the maximum possible sum of heights of 'beautiful' towers. We're given an array `maxHeights` where `maxHeights[i]` represents the maximum possible height for the tower at index `i`. A configuration of towers is considered beautiful if two conditions are met: (1) the height of each tower `heights[i]` must be between 1 and `maxHeights[i]` (inclusive), and (2) the `heights` array must be a mountain array. A mountain array is one that strictly increases up to a peak and then strictly decreases (or remains constant) after the peak. The goal is to find a beautiful configuration of tower heights such that the sum of the tower heights is maximized.
Solution Approach
The solution uses a dynamic programming approach aided by monotonic stacks to efficiently calculate the maximum sum of heights. It calculates two arrays: `prefix_sum` and `suffix_sum`. `prefix_sum[i]` stores the maximum possible sum of heights for towers from index 0 to `i` such that the heights are increasing up to index `i` (or staying the same), where `maxHeights[i]` is the height of tower at index `i`. `suffix_sum[i]` stores the maximum possible sum of heights for towers from index `i` to `n-1` such that the heights are decreasing from index `i` (or staying the same), where `maxHeights[i]` is the height of tower at index `i`. We iterate through each index `i` as a potential peak, and the total sum for that peak is calculated as `prefix_sum[i] + suffix_sum[i] - maxHeights[i]`. The maximum of these sums across all possible peaks is then returned.
Step-by-Step Algorithm
- Step 1: Initialize `prefix_sum` and `suffix_sum` arrays of size `n` with all elements set to 0.
- Step 2: Calculate the `prefix_sum` array using a monotonic stack. Iterate from left to right. For each index `i`, pop elements from the stack that are greater than or equal to `maxHeights[i]`. If the stack is empty after popping, it means that `maxHeights[i]` is the smallest element encountered so far, and `prefix_sum[i]` is `(i + 1) * maxHeights[i]`. Otherwise, `prefix_sum[i]` is the sum up to previous element in stack `prefix_sum[stack[-1]] + (i - stack[-1]) * maxHeights[i]`.
- Step 3: Calculate the `suffix_sum` array using a monotonic stack. Iterate from right to left. For each index `i`, pop elements from the stack that are greater than or equal to `maxHeights[i]`. If the stack is empty after popping, it means that `maxHeights[i]` is the smallest element encountered so far, and `suffix_sum[i]` is `(n - i) * maxHeights[i]`. Otherwise, `suffix_sum[i]` is the sum down from next element in stack `suffix_sum[stack[-1]] + (stack[-1] - i) * maxHeights[i]`.
- Step 4: Iterate through the array and for each `i`, calculate `total_sum = prefix_sum[i] + suffix_sum[i] - maxHeights[i]`. This removes double counting `maxHeights[i]`.
- Step 5: Keep track of the maximum `total_sum` and return it.
Key Insights
- Insight 1: The core idea is to iterate through each possible peak in the `maxHeights` array and calculate the sum of heights assuming that index is the peak.
- Insight 2: To efficiently calculate the sum for a given peak, we can precompute prefix and suffix sums that represent the sum of heights increasing up to that index (prefix) and decreasing down from that index (suffix).
- Insight 3: Monotonic stack can be used to determine the height of towers to the left and right of a considered peak, so that `height[i] <= height[i+1]` before peak and `height[i] >= height[i+1]` after peak.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Stack, Monotonic Stack.
Companies
Asked at: Salesforce.