Minimum Cost to Connect Sticks
A medium Heap problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Heap
- Sheets
- 1
- Core for
- 9 roles
- Platform
- LeetCode
The problem
You have a collection of sticks with positive integer lengths. In each step, pick any two sticks and connect them into one. The cost of connecting two sticks equals the sum of their lengths. Continue until all sticks are connected into one. Return the minimum possible total cost.
Example 1
- Input
- sticks = [2, 4, 3]
- Output
- 14
- Why
- Connect 2 and 3 (cost 5), sticks become [5, 4]. Connect 5 and 4 (cost 9), sticks become [9]. Total cost = 5 + 9 = 14.
Example 2
- Input
- sticks = [1, 8, 3, 5]
- Output
- 30
- Why
- Connect 1 and 3 (cost 4), sticks become [4, 8, 5]. Connect 4 and 5 (cost 9), sticks become [9, 8]. Connect 9 and 8 (cost 17), sticks become [17]. Total cost = 4 + 9 + 17 = 30.
Example 3
- Input
- sticks = [5]
- Output
- 0
- Why
- Only one stick exists, no connections needed. Total cost is 0.
Constraints
- 1 <= sticks.length <= 10^4
- 1 <= sticks[i] <= 10^4
How to think about it
Updated 2026-09-09Every time two sticks are combined, their lengths contribute to the current step's cost and every subsequent combination involving that merged stick. The sticks combined earliest accumulate into the running total most frequently, so the two shortest available sticks must always be combined first — the exact principle behind Huffman coding.
Approaches, worst first
Sort and re-insert linearly
time O(n^2) · space O(n)
Sort the array. Pop the two smallest, compute their sum, add it to total cost, and re-insert the sum in sorted position using linear scanning. Linear insertion across n merges yields quadratic overall time.
Min-heap simulationWrite this one
time O(n log n) · space O(n)
Insert all stick lengths into a min-heap. While more than one stick remains, pop the two smallest, sum them, add the sum to the total cost accumulator, and push the merged stick back into the heap.
Where people lose marks · 3
- A single stick requires 0 connections. If sticks.length is 1, the cost is 0, not sticks[0]. Your loop must run while heap size > 1, not while heap is non-empty.
- Confusing the step cost with the total accumulated cost. You must add the combined sum both to the overall total and back into the heap as a new stick.
- Re-sorting the whole array after every merge instead of using a priority queue or two-queue merge approach.
The theory behind it
Heap — the ground this problem stands on. All Heap problems
What Heap is
A heap is a specialized tree that keeps only the single most extreme item at the very top. In a min-heap, every parent node is smaller than its children, so the smallest element in the entire collection sits immediately at the root. Unlike a binary search tree, a heap does not keep all items in full sorted order. It maintains only a partial order, making it fast at giving you the single smallest or largest item without spending time sorting everything else.
When to reach for it
Reach for a heap when a problem asks for the top k largest elements, the kth smallest value, or a running median from a stream of numbers. Signals include phrases like continuously finding the cheapest item, merging k sorted linked lists, or scheduling tasks with priorities. Whenever you need repeated access to the minimum or maximum value while items are added and removed dynamically, a priority heap is the tool.
How the pattern works
To find the k largest elements, keep a min-heap of fixed size k. Push incoming numbers into the heap; whenever the heap size grows past k, pop the top item, which is the smallest among them. After processing all elements, only the k largest remain. For a running median, balance two heaps: a max-heap holding the smaller half of numbers and a min-heap holding the larger half. In code, heaps are stored compactly as flat arrays where a node at index i has children at indices 2i plus 1 and 2i plus 2.
What each operation costs
| Operation | Time |
|---|---|
| read the minimum or maximum element | O(1) |
| insert a new element and sift into position | O(log n) |
| remove the top element and sift down | O(log n) |
| build a heap from an array of n items | O(n) |
What usually goes wrong with Heap
- Using a max-heap instead of a min-heap when keeping the k largest elements, causing the largest values to be evicted while small items stay behind.
- Assuming that extracting elements by iterating over the backing array yields sorted order, without popping items from the heap one by one.
- Forgetting that standard language libraries provide a min-heap by default, leading to wrong answers when a max-heap was required.
Which roles need this problem
Heap is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 11 more roles, including SDE / Backend Engineer, Data Engineer, ML Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Heap problems
Problem set and role mapping as of .