Untitled

 avatar
unknown
plain_text
24 days ago
494 B
31
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