sweet1cris

Untitled

Feb 10th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     /**
  4.      * @param numbers: the numbers
  5.      * @return: the minimum cost
  6.      */
  7.     public int mergeNumber(int[] numbers) {
  8.         // Write your code here
  9.         Queue<Integer> Q = new PriorityQueue<>();
  10.         for(int i = 0; i < numbers.length; i++) {
  11.             Q.add(numbers[i]);
  12.         }
  13.         int ans = 0;
  14.         while(Q.size() > 1) {
  15.             int a = Q.poll();
  16.             int b = Q.poll();
  17.             ans += a + b;
  18.             Q.add(a + b);
  19.         }
  20.         return ans;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment