Join Cable Segments at the Lowest Total Splice Cost
Implement minSpliceCost
A cable is delivered in several segments of known lengths. Two segments can be spliced into one, and a splice costs the sum of the two lengths it joins. Find the minimum total cost to splice every segment into a single cable.
The order matters: every splice creates a longer segment that will be charged again in later splices. Always splicing the two shortest segments first keeps the repeatedly-charged lengths as small as possible. A min-heap hands over the two shortest segments in O(log n) instead of re-sorting the whole pool each round.
Example 1:
Input: segments = [7,2,5,9]
Output: 44
Example 2:
Input: segments = [10]
Output: 0
Example 3:
Input: segments = [6,6]
Output: 12
+ 9 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ segments.length ≤ 50 - ●
1 ≤ segments[i] ≤ 1000 - ●
Splicing two segments of lengths a and b costs a + b and produces one segment of length a + b - ●
Splice until a single cable remains; return the minimum total cost (0 if there is only one segment to begin with)
segments =