Skip to content

Commit 416c073

Browse files
authored
Create main.cpp
1 parent e321f4d commit 416c073

File tree

1 file changed

+28
-0
lines changed
  • 19 - Heap Data Structure Problems/07 - Minimum Cost of Ropes

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
pq.pop();
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

Comments
 (0)