LeetCode-in-Java

2866. Beautiful Towers II

Medium

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:

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:

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:

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:

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:

Solution

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    public long maximumSumOfHeights(List<Integer> mH) {
        int n = mH.size();
        Deque<Integer> st = new LinkedList<>();
        int[] prevSmaller = new int[n + 1];
        for (int i = 0; i < n; i++) {
            while (!st.isEmpty() && mH.get(st.peek()) >= mH.get(i)) {
                st.pop();
            }
            if (st.isEmpty()) {
                prevSmaller[i] = -1;
            } else {
                prevSmaller[i] = st.peek();
            }
            st.push(i);
        }
        st.clear();
        int[] nextSmaller = new int[n + 1];
        for (int i = n - 1; i >= 0; i--) {
            while (!st.isEmpty() && mH.get(st.peek()) >= mH.get(i)) {
                st.pop();
            }
            if (st.isEmpty()) {
                nextSmaller[i] = n;
            } else {
                nextSmaller[i] = st.peek();
            }
            st.push(i);
        }
        long[] leftSum = new long[n];
        leftSum[0] = mH.get(0);
        for (int i = 1; i < n; ++i) {
            int prevSmallerIdx = prevSmaller[i];
            int equalCount = i - prevSmallerIdx;
            leftSum[i] = ((long) equalCount * mH.get(i));
            if (prevSmallerIdx != -1) {
                leftSum[i] += leftSum[prevSmallerIdx];
            }
        }
        long[] rightSum = new long[n];
        rightSum[n - 1] = mH.get(n - 1);
        for (int i = n - 2; i >= 0; i--) {
            int nextSmallerIdx = nextSmaller[i];
            int equalCount = nextSmallerIdx - i;
            rightSum[i] = ((long) equalCount * mH.get(i));
            if (nextSmallerIdx != n) {
                rightSum[i] += rightSum[nextSmallerIdx];
            }
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            long totalSum = leftSum[i] + rightSum[i] - mH.get(i);
            ans = Math.max(ans, totalSum);
        }
        return ans;
    }
}