Skip to content

Commit ef47dbc

Browse files
authored
Create maximize-the-total-height-of-unique-towers.cpp
1 parent 276ec9f commit ef47dbc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
// sort, greedy
5+
class Solution {
6+
public:
7+
long long maximumTotalSum(vector<int>& maximumHeight) {
8+
sort(begin(maximumHeight), end(maximumHeight), greater<int>());
9+
int64_t result = 0;
10+
int prev = maximumHeight.front() + 1;
11+
for (const auto& x : maximumHeight) {
12+
prev = min(x, prev - 1);
13+
if (prev == 0) {
14+
return -1;
15+
}
16+
result += prev;
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)