Beautiful Towers II
MEDIUMDescription
You are given a 0-indexed array maxHeights of n integers.
You are tasked with building n towers in the coordinate line. The ith 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]heightsis 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]
Return the maximum possible sum of heights of a beautiful configuration of towers.
Example 1:
Input: maxHeights = [5,3,4,1,1] Output: 13 Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
Example 2:
Input: maxHeights = [6,5,3,9,2,7] Output: 22 Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
Example 3:
Input: maxHeights = [3,2,5,5,2,3] Output: 18 Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
Constraints:
1 <= n == maxHeights.length <= 1051 <= maxHeights[i] <= 109
Approaches
Checkout 2 different approaches to solve Beautiful Towers II. Click on different approaches to view the approach and algorithm in detail.
Dynamic Programming with Monotonic Stack
The O(N^2) brute-force approach is slow because it repeatedly calculates sums for overlapping subarrays. We can optimize this by pre-calculating these sums. The key insight is that the total sum for a mountain with a peak at i can be decomposed into two independent subproblems: the maximum sum of a non-decreasing sequence ending at i and the maximum sum of a non-increasing sequence starting at i. These two sets of sums (for all i) can be computed efficiently in O(N) time using a monotonic stack. After pre-computation, we can find the maximum total sum in a final O(N) pass.
Algorithm
- Define two arrays,
leftSumandrightSum, of sizento store precomputed sums. - Calculate
leftSum:leftSum[i]will store the maximum sum of a valid non-decreasing sequence of towers onmaxHeights[0...i]withias the peak.- Use a monotonic stack (storing indices of
maxHeightswith increasing values) and iterate fromi = 0ton-1. - For each
i, pop from the stack whilemaxHeights[stack.peek()] >= maxHeights[i]. - The sum
leftSum[i]can then be calculated in O(1) using the previously computed sum for the new stack top (leftSum[p]) and the valuemaxHeights[i].
- Use a monotonic stack (storing indices of
- Calculate
rightSum:rightSum[i]will store the maximum sum of a valid non-increasing sequence of towers onmaxHeights[i...n-1]withias the peak.- This is symmetric to the
leftSumcalculation. Use a monotonic stack and iterate fromi = n-1down to0.
- This is symmetric to the
- Combine Results: The maximum sum for a mountain with peak
iisleftSum[i] + rightSum[i] - maxHeights[i](subtractingmaxHeights[i]because it's included in both sums). - Iterate through all
ifrom0ton-1, calculate this combined sum, and find the maximum value. This will be the final answer.
This approach is based on dynamic programming and a monotonic stack. We pre-calculate two arrays:
-
leftSum[i]: The maximum sum of a beautiful tower configuration for the prefixmaxHeights[0...i]where the heights are non-decreasing (i.e.,iis the peak of this prefix). We can compute this for alliin O(N) time. We iterate from left to right, maintaining a monotonic stack of indices wheremaxHeightsvalues are increasing. For eachi, we find the previous indexpwheremaxHeights[p] < maxHeights[i]. All towers betweenpandiwill have heightmaxHeights[i]. The total sumleftSum[i]can be derived fromleftSum[p]. -
rightSum[i]: Similarly, this is the maximum sum for the suffixmaxHeights[i...n-1]where heights are non-increasing. This is calculated symmetrically by iterating from right to left.
Once both leftSum and rightSum arrays are populated, we can find the maximum possible sum for a mountain with a peak at index i by the formula: leftSum[i] + rightSum[i] - maxHeights.get(i). We subtract maxHeights.get(i) because it was counted in both leftSum[i] and rightSum[i]. The final answer is the maximum value of this expression over all i from 0 to n-1.
import java.util.List;
import java.util.Stack;
class Solution {
public long maximumSumOfHeights(List<Integer> maxHeights) {
int n = maxHeights.size();
long[] leftSum = new long[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
long currentMaxHeight = maxHeights.get(i);
while (!stack.isEmpty() && maxHeights.get(stack.peek()) >= currentMaxHeight) {
stack.pop();
}
if (stack.isEmpty()) {
leftSum[i] = (long)(i + 1) * currentMaxHeight;
} else {
int prevSmallerIndex = stack.peek();
leftSum[i] = leftSum[prevSmallerIndex] + (long)(i - prevSmallerIndex) * currentMaxHeight;
}
stack.push(i);
}
long[] rightSum = new long[n];
stack.clear();
for (int i = n - 1; i >= 0; i--) {
long currentMaxHeight = maxHeights.get(i);
while (!stack.isEmpty() && maxHeights.get(stack.peek()) >= currentMaxHeight) {
stack.pop();
}
if (stack.isEmpty()) {
rightSum[i] = (long)(n - i) * currentMaxHeight;
} else {
int nextSmallerIndex = stack.peek();
rightSum[i] = rightSum[nextSmallerIndex] + (long)(nextSmallerIndex - i) * currentMaxHeight;
}
stack.push(i);
}
long maxTotalSum = 0;
for (int i = 0; i < n; i++) {
maxTotalSum = Math.max(maxTotalSum, leftSum[i] + rightSum[i] - maxHeights.get(i));
}
return maxTotalSum;
}
}
Complexity Analysis
Pros and Cons
- Highly efficient with a linear time complexity.
- Passes the given constraints with ease.
- More complex to understand and implement correctly compared to the brute-force approach.
- Requires knowledge of the monotonic stack data structure and its application.
Code Solutions
Checking out 3 solutions in different languages for Beautiful Towers II. Click on different languages to view the code.
class Solution {
public
long maximumSumOfHeights(List<Integer> maxHeights) {
int n = maxHeights.size();
Deque<Integer> stk = new ArrayDeque<>();
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, -1);
Arrays.fill(right, n);
for (int i = 0; i < n; ++i) {
int x = maxHeights.get(i);
while (!stk.isEmpty() && maxHeights.get(stk.peek()) > x) {
stk.pop();
}
if (!stk.isEmpty()) {
left[i] = stk.peek();
}
stk.push(i);
}
stk.clear();
for (int i = n - 1; i >= 0; --i) {
int x = maxHeights.get(i);
while (!stk.isEmpty() && maxHeights.get(stk.peek()) >= x) {
stk.pop();
}
if (!stk.isEmpty()) {
right[i] = stk.peek();
}
stk.push(i);
}
long[] f = new long[n];
long[] g = new long[n];
for (int i = 0; i < n; ++i) {
int x = maxHeights.get(i);
if (i > 0 && x >= maxHeights.get(i - 1)) {
f[i] = f[i - 1] + x;
} else {
int j = left[i];
f[i] = 1L * x * (i - j) + (j >= 0 ? f[j] : 0);
}
}
for (int i = n - 1; i >= 0; --i) {
int x = maxHeights.get(i);
if (i < n - 1 && x >= maxHeights.get(i + 1)) {
g[i] = g[i + 1] + x;
} else {
int j = right[i];
g[i] = 1L * x * (j - i) + (j < n ? g[j] : 0);
}
}
long ans = 0;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, f[i] + g[i] - maxHeights.get(i));
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Beautiful Towers II
Similar Questions
5 related questions you might find useful
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.