leetcode_1167
Solutions
class Solution {
public:
int connectSticks(vector<int>& sticks) {
using PQ = priority_queue<int, vector<int>, greater<>>;
PQ pq(sticks.begin(), sticks.end());
int cost = 0;
while (pq.size() >= 2) {
auto min1 = pq.top(); pq.pop();
auto min2 = pq.top(); pq.pop();
cost += min1 + min2;
pq.push(min1 + min2);
}
return cost;
}
};Last updated