---
# System prepended metadata

title: Heap
tags: [Week_2, Heap]

---

###### tags: `Week_2`, `Heap`

# Heap

## 1046. [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/)
Beny
(Too New)
```csharp=
public class Solution {
    public int LastStoneWeight(int[] stones) {
        PriorityQueue<int,int> qstones  = new PriorityQueue<int,int>(stones.Select(x=>(x,-x)));
        while (qstones.Count > 1){
             int num = qstones.Dequeue() - qstones.Dequeue();
             if (num != 0) qstones.Enqueue(num, -num);
        }
           
        return (qstones.Count == 0) ? 0 : qstones.Peek();
    }  
}
```
Beny
(second try)
```csharp=
public class solution{
      public int LastStoneWeight(int[] stones)
        {           
            List<int> list = new List<int>();
            foreach (int val in stones)
            {
                list.Add(val);
            }

            while (list.Count > 1)
            { 
                list = list.OrderByDescending(o => o).ToList(); 
                int y = list[0];
                int x = list[1];
                if (x == y)
                {
                    list.RemoveAt(0); 
                    list.RemoveAt(0); 
                }
                else
                {
                    list.RemoveAt(1); 
                    list[0] = y - x; 
                }
            }
            return list.Count == 1 ? list[0] : 0;
        }

}
```