Skip to content

Commit 94da056

Browse files
authored
Update maximal-score-after-applying-k-operations.cpp
1 parent 3bff63c commit 94da056

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

C++/maximal-score-after-applying-k-operations.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ class Solution {
99
return (a + b - 1) / b;
1010
};
1111

12+
int64_t result = 0;
13+
priority_queue<int> max_heap(cbegin(nums), cend(nums));
14+
while (k-- && !empty(max_heap)) {
15+
const auto x = max_heap.top(); max_heap.pop();
16+
result += x;
17+
const auto nx = ceil_divide(x, 3);
18+
if (!nx) {
19+
continue;
20+
}
21+
max_heap.emplace(nx);
22+
}
23+
return result;
24+
}
25+
};
26+
27+
// Time: O(n + klogn)
28+
// Space: O(n)
29+
// heap
30+
class Solution2 {
31+
public:
32+
long long maxKelements(vector<int>& nums, int k) {
33+
const auto& ceil_divide = [](const auto& a, const auto& b) {
34+
return (a + b - 1) / b;
35+
};
36+
1237
int64_t result = 0;
1338
priority_queue<int> max_heap(cbegin(nums), cend(nums));
1439
while (k--) {

0 commit comments

Comments
 (0)