We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e321f4d commit 416c073Copy full SHA for 416c073
19 - Heap Data Structure Problems/07 - Minimum Cost of Ropes/main.cpp
@@ -0,0 +1,28 @@
1
+class Solution {
2
+ public:
3
+
4
+ long long minCost(vector<long long>& arr) {
5
6
+ priority_queue<long long, vector<long long>, greater<long long>> pq;
7
8
+ for(int i = 0; i < arr.size(); i++){
9
+ pq.push(arr[i]);
10
+ }
11
12
+ long long cost = 0;
13
+ while(pq.size() > 1){
14
+ long long a = pq.top();
15
+ pq.pop();
16
17
+ long long b = pq.top();
18
19
20
+ long long sum = a + b;
21
+ cost += sum;
22
23
+ pq.push(sum);
24
25
26
+ return cost;
27
28
+};
0 commit comments