Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int connectSticks(vector<int>& sticks) {
- int ans = 0;
- priority_queue<int, vector<int>, greater<int>> pq(sticks.begin(), sticks.end());
- while(pq.size() > 1){
- int len1 = pq.top(); pq.pop();
- int len2 = pq.top(); pq.pop();
- ans += (len1+len2);
- pq.push(len1+len2);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement