Beautiful Towers II

MEDIUM

Description

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. 1 <= heights[i] <= maxHeights[i]
  2. 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]

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 <= 105
  • 1 <= 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, leftSum and rightSum, of size n to store precomputed sums.
  • Calculate leftSum: leftSum[i] will store the maximum sum of a valid non-decreasing sequence of towers on maxHeights[0...i] with i as the peak.
    • Use a monotonic stack (storing indices of maxHeights with increasing values) and iterate from i = 0 to n-1.
    • For each i, pop from the stack while maxHeights[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 value maxHeights[i].
  • Calculate rightSum: rightSum[i] will store the maximum sum of a valid non-increasing sequence of towers on maxHeights[i...n-1] with i as the peak.
    • This is symmetric to the leftSum calculation. Use a monotonic stack and iterate from i = n-1 down to 0.
  • Combine Results: The maximum sum for a mountain with peak i is leftSum[i] + rightSum[i] - maxHeights[i] (subtracting maxHeights[i] because it's included in both sums).
  • Iterate through all i from 0 to n-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:

  1. leftSum[i]: The maximum sum of a beautiful tower configuration for the prefix maxHeights[0...i] where the heights are non-decreasing (i.e., i is the peak of this prefix). We can compute this for all i in O(N) time. We iterate from left to right, maintaining a monotonic stack of indices where maxHeights values are increasing. For each i, we find the previous index p where maxHeights[p] < maxHeights[i]. All towers between p and i will have height maxHeights[i]. The total sum leftSum[i] can be derived from leftSum[p].

  2. rightSum[i]: Similarly, this is the maximum sum for the suffix maxHeights[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

Time Complexity: O(N). Calculating `leftSum` takes O(N), `rightSum` takes O(N), and the final pass to combine them takes O(N). Each element is pushed and popped from the stack at most once.Space Complexity: O(N), for storing the `leftSum` and `rightSum` arrays, and for the stack which can grow up to size N in the worst case.

Pros and Cons

Pros:
  • Highly efficient with a linear time complexity.
  • Passes the given constraints with ease.
Cons:
  • 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



Data Structures:

ArrayStackMonotonic Stack

Companies:

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.