package leetcode;

/**
 * https://leetcode.com/problems/last-stone-weight/
 */
public class Problem1046 {
    public int lastStoneWeight(int[] stones) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1046 prob = new Problem1046();
        System.out.println(prob.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1})); // 1
    }
}

package leetcode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * https://leetcode.com/problems/last-stone-weight/
 */
public class Problem1046 {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
        for (int stone : stones) {
            queue.add(stone);
        }
        while (!queue.isEmpty()) {
            Integer x = queue.poll();
            if (queue.isEmpty()) {
                return x;
            }
            Integer y = queue.poll();
            int diff = Math.max(x, y) - Math.min(x, y);
            if (diff > 0) {
                queue.add(diff);
            }
        }
        return 0;
    }
}

Solve problem 1046