Trains the technique from
LeetCode 1167Minimum Cost to Connect SticksThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A pile of rods has the lengths rods. Joining two rods costs their two lengths added together and leaves a single rod of that length in their place.
Join rods until only one remains. Return the smallest total cost.
Example 1
Joining 1 and 2 costs three, joining that with 3 costs six, and joining that with 4 costs ten, nineteen in all. Any other order charges the short rods more times over.
Example 2
One join of the two rods, charging their two lengths.
Example 3
Each join takes the two shortest rods left, so the four joins cost three, seven, fifteen and thirty-one.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def connect_sticks(rods: list[int]) -> int:public int connectSticks(int[] rods)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.