给你一个长度为?
n
?下标从?0?开始的整数数组?maxHeights
?。你的任务是在坐标轴上建?
n
?座塔。第?i
?座塔的下标为?i
?,高度为?heights[i]
?。如果以下条件满足,我们称这些塔是?美丽?的:
1 <= heights[i] <= maxHeights[i]
heights
?是一个?山脉?数组。如果存在下标?
i
?满足以下条件,那么我们称数组?heights
?是一个?山脉?数组:
- 对于所有?
0 < j <= i
?,都有?heights[j - 1] <= heights[j]
- 对于所有?
i <= k < n - 1
?,都有?heights[k + 1] <= heights[k]
请你返回满足?美丽塔?要求的方案中,高度和的最大值?。
class Solution {
public long maximumSumOfHeights(List<Integer> maxHeights) {
int n = maxHeights.size();
int[] a = maxHeights.stream().mapToInt(i -> i).toArray();
long ans = 0L;
for (int num = 0; num < n; num++) {
if(num < n - 1 && a[num] < a[num + 1]){
continue;
}
if(num > 0 && a[num] <= a[num - 1]) {
continue;
}
int left = a[num];
int right = left;
long res = left;
for (int i = num - 1; i >= 0; i--) {
int temp = a[i];
if (temp > left) {
res += left;
} else {
left = temp;
res += temp;
}
}
for (int i = num + 1; i < n; i++) {
int temp = a[i];
if (temp > right) {
res += right;
} else {
res += temp;
right = temp;
}
}
ans = Math.max(ans,res);
}
return ans;
}
}