Ctrl K

Splicing Ropes

Medium
A warehouse holds ropes whose lengths are given in ropes. They all need to be spliced into a single long rope. At each step you can splice any two ropes of lengths x and y into one of length x + y, and that splice costs x + y. You keep going until only one rope is left. Return the minimum total cost of the job.
Example 1
ropes=[6, 2, 2, 3] → 24

2 + 2 = 4 (pay 4), then 3 + 4 = 7 (pay 7), then 6 + 7 = 13 (pay 13). Total 24.

Example 2
ropes=[9] → 0

There is only one rope, nothing to splice.

Example 3
ropes=[4, 4, 4] → 20
Constraints
  • 1 ≤ len(ropes) ≤ 10⁵
  • 1 ≤ ropes[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumSpread Out the Labels MediumCross Out Half the List Medium
loading Python…
Input
ropes=[6, 2, 2, 3]
Expected
24

Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.