I think the greedy method of taking the current minimum sized 2 ropes and
tying them will do. Consider this algo:-
int getMinCost(){
priority_queue pq;
insert all thread sizes to pq;
int sum=0;
while(!pq.empty()){
int a=pq.extractmin(); //O(logn)
int b=pq.extractmin();
sum+=a+b;
pq.push_back(a+b);
}
return sum;
}
Time: O(nlogn)
Let me know if this fails in some case.
Anurag
On Mon, Mar 28, 2011 at 12:11 PM, bittu <[email protected]> wrote:
> you are given n ropes,maybe of different length. the cost of tying two
> ropes is the sum of their lengths.Find a way to tie these ropes
> together so that the cost is minimum.
>
>
>
> Thanks
> Shashank
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.