Posted on

Description

Submission

class Solution {
public:
    int connectSticks(vector<int>& sticks) {
        priority_queue<int, vector<int>, greater<>> pq;

        for(auto x: sticks) {
            pq.push(x);
        }

        int ret = 0;
        while(pq.size() != 1) {
            int x = pq.top();
            pq.pop();
            int y = pq.top();
            pq.pop();
            pq.push(x+y);
            ret += (x+y);
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *