import java.util.*;
public class Main {
public static int minRopeCost(int[] arr, int n) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
// add all elements to the min-heap
for(int i=0; i<n; i++) {
pq.add(arr[i]);
}
int res = 0;
// keep combining two smallest el.
while(pq.size() > 1)
{
int first = pq.poll(); // 1st smallest elements
int second = pq.poll(); // 2nd smallest elements
res += first+second; // add their combined cost;
int new_rope = first + second; // store the new rope
pq.add(new_rope); // push the new rope back into the heap
}
// return total cost
return res;
}
public static void main(String[] args) {
int[] rope = {3, 2, 4, 6};
int n = rope.length;
System.out.println("Total cost for connecting ropes is " + minRopeCost(rope, n));
}
}
problems i faced:
1. Main.java:43: error: reached end of file while parsing
}
^
1 error
FIX:
Make sure main , class and function braces are closed properly.