---
# System prepended metadata

title: 1046. Last Stone Weight
tags: [Heap]

---

---
title: 1046. Last Stone Weight
tags: Heap
description: share source code.
---

# 1046. Last Stone Weight

```java

// used priorityQueue to find top two
class Solution {
    
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> q = new PriorityQueue<>( (a, b) -> b - a );
        for(int i = 0; i < stones.length; i++){
            q.offer(stones[i]);
        }
        
        while(q.size() > 1){
            int a = q.poll();
            int b = q.poll();
            if(a - b > 0){
                q.offer(a - b);
            }
        }
        
        return q.isEmpty() ? 0 : q.peek();
    }
}
```

