Last Stone Weight
一个数组,有放回的拿出2个最大数相减,结果放回,问最后结果。因为是有放回,所以肯定结果是一个数。除非数组是空的。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public int lastStoneWeight(int[] stones) { PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder()); for(int s : stones) pq.add(s); int size = pq.size(); while(pq.size() > 1){ int x = pq.poll(); int y = pq.poll(); pq.add(Math.abs(x-y)); } return pq.peek(); } } |