LeetCode 1167, Easy. Topics: Array, Greedy, Heap (Priority Queue). View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:
Problem
You have some number of sticks with positive integer lengths. These lengths are given as an array sticks, where sticks[i] is the length of the i<sup>th</sup> stick.
You can connect any two sticks of lengths x and y into one stick by paying a cost of x + y. You must connect all the sticks until there is only one stick remaining.
Return the minimum cost of connecting all the given sticks into one stick in this way.
Examples
Constraints
- 1 <= sticks.length <= 10^4
- 1 <= sticks[i] <= 10^4
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026