Interview Bootcamp

0% completed

Solution: Minimum Cost to Connect Sticks(medium)

Problem Statement

Given a collection of sticks with different lengths. To combine any two sticks, there's a cost involved, which is equal to the sum of their lengths.

Connect all the sticks into a single one with the minimum possible cost. Remember, once two sticks are combined, they form a single stick whose length is the sum of the lengths of the two original sticks.

Examples

    • Input: [2, 4, 3]
    • Expected Output: 14
    • Justification: Combine sticks 2 and 3 for a cost of 5. Now, we have sticks [4,5]. Combine these at a cost of 9. Total cost = 5 + 9 = 14

.....

.....

.....

Like the course? Get enrolled and start learning!
J

Jimmy

· 2 years ago

The expected output for example #2 should be 27 since there's a cost of 27 involved.

Rajan Waliya

Rajan Waliya

· 2 years ago

In order to solve this, I had to realize that this is under 'heap' set of problems.