Skip to content

Commit

Permalink
cost of connecting n ropes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 9, 2024
1 parent 9a8b01b commit e6e2b32
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Heap/Nropes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Heap;
import java.util.*;
public class Nropes {
public static void main(String[] args) {
int ropes[]={2,3,3,4,6};
PriorityQueue<Integer>p=new PriorityQueue<>();
for(int i=0; i<ropes.length;i++){
p.add(ropes[i]);
}
int cost=0;
while(p.size()>1){
int min=p.remove();
int min2=p.remove();
cost+=min+min2;
p.add(min+min2);
}
System.out.println("Cost of connecting n ropes ="+cost);
}
}

0 comments on commit e6e2b32

Please sign in to comment.