Advertisement
nikunjsoni

1167

May 29th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int connectSticks(vector<int>& sticks) {
  4.         int ans = 0;
  5.         priority_queue<int, vector<int>, greater<int>> pq(sticks.begin(), sticks.end());
  6.         while(pq.size() > 1){
  7.             int len1 = pq.top(); pq.pop();
  8.             int len2 = pq.top(); pq.pop();
  9.             ans += (len1+len2);
  10.             pq.push(len1+len2);
  11.         }
  12.         return ans;
  13.     }
  14. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement