Skip to main content
Intermediatecoding Verified

Merging two devices costs the sum of their sizes, and the merged device can be merged again. Find the minimum total cost to merge N devices into one (Connect N ropes with minimum cost).

ReportedSource: GeeksforGeeks interview experience: Amazon SDE intern 2024

Quick answerFree

Min-heap: repeatedly pop the two smallest, add their sum to the cost, push the sum back. Optimal by the Huffman argument. O(n log n).

Detailed answer· ~100 words

Interview Prep

Unlock the full answer with Interview Prep

Get a ~100-word model answer and a runnable reference solution, plus 61 more DSA questions.

Also in Pro (₹499 / 90 days).

Reference solution· runs in the browser sandbox

Interview Prep
Unlock with Interview Prep

+ 61 more DSA questions in Interview Prep, each with the full answer.

Discussion