Problem:美丽塔 I
思路:枚举每个峰的位置
进阶版:进阶
Accode:
class Solution {
public:
long long maximumSumOfHeights(vector<int>& heights) {
long long ans = 0;
for(int i=0;i<heights.size();i++){
long long sum = heights[i];
int minn = heights[i];
for(int j=i-1;j>=0;j--){
if(heights[j]<=minn){
sum+=heights[j];
minn = heights[j];
}
else{
sum+=minn;
}
}
minn = heights[i];
for(int j=i+1;j<heights.size();j++){
if(heights[j]>=minn){
sum+=minn;
}
else{
sum+=heights[j];
minn = heights[j];
}
}
ans = max(ans,sum);
}
return ans;
}
};
时间复杂度:$o(n)$