Input: maxHeights =[5,3,4,1,1]Output: 13Explanation: 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.
Input: maxHeights =[6,5,3,9,2,7]Output: 22Explanation: 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.
Input: maxHeights =[3,2,5,5,2,3]Output: 18Explanation: 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,forthis 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.
For each possible peak, we want to maximize the sum of the mountain array, where each height does not exceed its maxHeights value and is non-decreasing to the peak and non-increasing after. We use monotonic stacks to efficiently compute the best possible sum for each peak.
classSolution {
public:longlong maximumSumOfHeights(vector<int>& h) {
int n = h.size();
longlong ans =0;
for (int peak =0; peak < n; ++peak) {
vector<int> arr(n);
arr[peak] = h[peak];
for (int i = peak-1; i >=0; --i)
arr[i] = min(h[i], arr[i+1]);
for (int i = peak+1; i < n; ++i)
arr[i] = min(h[i], arr[i-1]);
longlong sum =0;
for (int x : arr) sum += x;
ans = max(ans, sum);
}
return ans;
}
};
classSolution {
publiclongmaximumSumOfHeights(int[] h) {
int n = h.length;
long ans = 0;
for (int peak = 0; peak < n; ++peak) {
int[] arr =newint[n];
arr[peak]= h[peak];
for (int i = peak-1; i >= 0; --i)
arr[i]= Math.min(h[i], arr[i+1]);
for (int i = peak+1; i < n; ++i)
arr[i]= Math.min(h[i], arr[i-1]);
long sum = 0;
for (int x : arr) sum += x;
ans = Math.max(ans, sum);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funmaximumSumOfHeights(h: IntArray): Long {
val n = h.size
var ans = 0Lfor (peak in0 until n) {
val arr = IntArray(n)
arr[peak] = h[peak]
for (i in peak-1 downTo 0) arr[i] = minOf(h[i], arr[i+1])
for (i in peak+1 until n) arr[i] = minOf(h[i], arr[i-1])
var sum = 0Lfor (x in arr) sum += x
ans = maxOf(ans, sum)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmaximumSumOfHeights(self, h: list[int]) -> int:
n = len(h)
ans =0for peak in range(n):
arr = [0]*n
arr[peak] = h[peak]
for i in range(peak-1, -1, -1):
arr[i] = min(h[i], arr[i+1])
for i in range(peak+1, n):
arr[i] = min(h[i], arr[i-1])
ans = max(ans, sum(arr))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmaximum_sum_of_heights(h: Vec<i32>) -> i64 {
let n = h.len();
letmut ans =0i64;
for peak in0..n {
letmut arr =vec![0; n];
arr[peak] = h[peak];
for i in (0..peak).rev() {
arr[i] = h[i].min(arr[i+1]);
}
for i in peak+1..n {
arr[i] = h[i].min(arr[i-1]);
}
let sum: i64= arr.iter().map(|&x| x asi64).sum();
ans = ans.max(sum);
}
ans
}
}