Skip to content

Commit 9034887

Browse files
authored
Create minimum-operations-to-exceed-threshold-value-ii.cpp
1 parent f5198f1 commit 9034887

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
// simulation, heap
5+
class Solution {
6+
public:
7+
int minOperations(vector<int>& nums, int k) {
8+
int result = 0;
9+
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> min_heap(cbegin(nums), cend(nums));
10+
while (!empty(nums)) {
11+
if (min_heap.top() >= k) {
12+
break;
13+
}
14+
const auto mn1 = min_heap.top(); min_heap.pop();
15+
const auto mn2 = min_heap.top(); min_heap.pop();
16+
min_heap.emplace(2 * mn1 + mn2);
17+
++result;
18+
}
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)