Skip to content

Latest commit

 

History

History
26 lines (26 loc) · 685 Bytes

File metadata and controls

26 lines (26 loc) · 685 Bytes
 public static int lastStoneWeight(int[] stones) {
        Queue<Integer> q=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i=0;i<stones.length;i++){
            q.offer(stones[i]);
        }
        while(q.size()>1){
            int y=q.poll();
            int x=q.poll();
            if(x!=y){
                q.offer(y-x);
            }
        }
        if(q.size()==0){
            return 0;
        }
        return q.peek();
    }