Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param numbers: the numbers
- * @return: the minimum cost
- */
- public int mergeNumber(int[] numbers) {
- // Write your code here
- Queue<Integer> Q = new PriorityQueue<>();
- for(int i = 0; i < numbers.length; i++) {
- Q.add(numbers[i]);
- }
- int ans = 0;
- while(Q.size() > 1) {
- int a = Q.poll();
- int b = Q.poll();
- ans += a + b;
- Q.add(a + b);
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment