Gamified challenge · Min-heap
ropes[i] is the length of rope i. The crew joins ropes two at a time: joining ropes of length a and b gives one rope of length a + b and costs a + b. They keep going until only one rope is left.
Write minCost(ropes) (Python min_cost(ropes)) that returns the smallest total cost of joining everything. With no ropes or a single rope nothing is joined, so the cost is 0.
Example: [4, 3, 2, 6] → join 2+3 (cost 5), then 4+5 (cost 9), then 6+9 (cost 15), total 29.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google