LC.P2865[美丽塔I]

方法一:枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public long maximumSumOfHeights(List<Integer> maxHeights) {
long ans = 0;
int n = maxHeights.size();
for (int i = 0; i < n; ++i) {
int height = maxHeights.get(i);
long t = height;
for (int j = i - 1; j >= 0; --j) {
height = Math.min(height, maxHeights.get(j));
t += height;
}
height = maxHeights.get(i);
for (int j = i + 1; j < n; ++j) {
height = Math.min(height, maxHeights.get(j));
t += height;
}
ans = Math.max(ans, t);
}
return ans;
}
}
  • 时间复杂度:$O(n^2)$
  • 空间复杂度:$O(1)$