P18 · 1 second · 256 MB
Connect Ropes
You are given rope lengths `lens`. Joining two ropes of lengths `x` and `y` costs `x+y` and produces a rope of length `x+y`. Repeat until one rope remains. Return the minimum total cost. If there are fewer than two ropes, the cost is 0.
function connectRopes(lens) → number
CONSTRAINTS
- 0 ≤ lens.length ≤ 200
- 1 ≤ lens[i] ≤ 10⁴
Input: lens = [4, 3, 2, 6]
Output: 29
Join 2+3 (cost 5), then 4+5 (cost 9), then 6+9 (cost 15). Total 29.
Input: lens = [1]
Output: 0
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.