Untitled
unknown
plain_text
9 months ago
494 B
34
Indexable
int minimumCost( int arr[])
{
int n = arr.length;
PriorityQueue<Integer>pq= new PriorityQueue<>(); // min Heap
// putting all the ropes inside the min heap
for(int i=0;i<n;i++ )
{
pq.add( arr[i] );
}
int ans=0;
while( pq.size()>1 )
{
int a = pq.remove();
int b= pq.remove();
int newRope = a+b;
ans+= newRope;
pq.add( newRope );
}
return ans;
}
Editor is loading...
Leave a Comment