File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,31 @@ class Solution {
9
9
return (a + b - 1 ) / b;
10
10
};
11
11
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
+
12
37
int64_t result = 0 ;
13
38
priority_queue<int > max_heap (cbegin (nums), cend (nums));
14
39
while (k--) {
You can’t perform that action at this time.
0 commit comments