> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/top-k-elements-heap-bucket-sort/1046.-last-stone-weight.md).

# 1046. Last Stone Weight

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mfv9_b6Pq0mxLPvR4LJ%2F-MfvQsbuTjLxpd2lDy4n%2Fimage.png?alt=media\&token=0b24f8ab-5747-4c81-ba6a-e13a49379e0a)

time: nlog(n)

space: O(n)

```java
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        for (int s : stones) {
            pq.offer(s);
        }
        while (pq.size() > 1) {
            pq.offer(pq.poll() - pq.poll());
        }
        return pq.poll();
    }
}
```
