---
title: "1046. Last Stone Weight"
url: "https://laigary.com/interview/coding/1046-last-stone-weight"
type: "note"
section: "coding"
date: "2023-12-28"
updated: "2023-12-28"
tags: ["Design", "Greedy", "Heap"]
---

# 1046. Last Stone Weight

[1046\. Last Stone Weight](https://leetcode.com/problems/last-stone-weight/)

```python
class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        heap = []
        for stone in stones:
            heapq.heappush(heap, -1 * stone)
        
        while len(heap) > 1:
            first = heapq.heappop(heap)
            second = heapq.heappop(heap)
            if first != second:
                heapq.heappush(heap, first - second)
        
        if len(heap) == 0:
            return 0
        else:
            return -1 * heap[0]
```
