Search⌘ K
AI Features

Challenge: Connecting n Pipes With Minimum Cost

Given n pipes, find the minimum cost of connecting them.

Problem statement

Implement a function that connects n pipes of different lengths, into one pipe. You can assume that the cost to connect two pipes is equal to the sum of their lengths. We need to connect the pipes with minimum cost.

Input

A list containing lengths of n pipes

Output

The total cost of connecting the pipes

Sample input

pipes = [4, 3, 2, 6]

Sample output

result = 29

Coding exercise

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!

Python 3.5
def min_cost(pipes):
"""
Calculates the minimum cost of connecting pipes
:param pipes: A list where its length is the number of pipes and indexes are the specific lengths of the pipes.
:return: The minimum cost
"""
# Write your code here!
pass