Loading
Loading
A stage crew has several lengths of cable and needs one continuous run. Splicing two cables together produces one cable whose length is their sum, and the splice costs exactly that combined length in labour.
Given the cable lengths, return the minimum total cost to splice them all into one cable. A single cable costs 0.
lengths = [4, 3, 2, 6]heap = {2, 3, 4, 6}startheap = {4, 5, 6}, cost 5splice 2 + 3heap = {6, 9}, cost 14splice 4 + 5heap = {15}, cost 29splice 6 + 929Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n log n) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Splice 2 + 3 = 5, then 4 + 5 = 9, then 6 + 9 = 15. Total 5 + 9 + 15 = 29.
Example 2
A single cable needs no splicing.