352. Minimum Cost to Connect Sticks

You have some sticks with positive integer lengths. You can connect any two sticks together to form a longer stick by paying a cost equal to the sum of their lengths. You must connect all the sticks into one single stick.

Return the minimum cost of connecting all the sticks.

Example 1:

Input: sticks = [2, 4, 3]

Output: 14

Explanation:

Connect sticks 2 and 3 → cost = 2 + 3 = 5 (sticks = [5, 4]),

Connect sticks 4 and 5 → cost = 4 + 5 = 9 (sticks = [9]),

Total cost = 5 + 9 = 14

Example 2:

Input: sticks = [1, 8, 3, 5]

Output: 30

Explanation:

Connect 1 and 3 → cost = 1 + 3 = 4 (sticks = [4, 8, 5]),

Connect 4 and 5 → cost = 4 + 5 = 9 (sticks = [9, 8]),

Connect 8 and 9 → cost = 8 + 9 = 17 (sticks = [17]),

Total cost = 4 + 9 + 17 = 30

Now Your Turn!

Pick the correct output for the given input

Input: sticks = [5, 2, 1, 6]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= sticks.length <= 104
  • 1 <= stcks[i] <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int connectSticks(vector<int>& sticks) {
// Your Code Goes Here
}
};
Test Case

Input:

Sticks