HarshCasper / NeoAlgo

Bringing all Data Structures and Algorithms under one Roof ⚡
MIT License
875 stars 1.05k forks source link

Added Minimum cost of ropes in C++ #7545

Closed CodesbyUnnati closed 2 years ago

CodesbyUnnati commented 3 years ago

Have you read the Contributing Guidelines on Pull Requests?

Yes

Description

There are given n ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to the sum of their lengths. We need to connect the ropes with minimum cost.

This approach is similar to Huffman Coding. We put the smallest ropes down the tree so that they can be repeated multiple times rather than the longer ropes.

Time Complexity: O(N log N), assuming that we use an O(N log N) sorting algorithm. Note that heap operations like insert and extract take O(Logn) time.

Auxiliary Complexity: O(n). The space required to store the values in min-heap

Checklist

Related Issues or Pull Requests

Issue: #7546