JS | Heap | Easy Understanding
Solution 1 - O(n^2logn)\n\n\nvar lastStoneWeight = function(stones) {\n\n    while(stones.length > 1) {\n        stones.sort((a, b) => a - b);\n        let x = 
Solution 1 - O(n^2*logn)\n\n```\nvar lastStoneWeight = function(stones) {\n\n    while(stones.length > 1) {\n        stones.sort((a, b) => a - b);\n        let x = stones.pop();\n        let y = stones.pop();\n        \n        if(x === y) continue;\n        else stones.push(Math.abs(x - y));\n    }\n    \n    return stones;\n}\n```\n\nSolution 2 - O(n*logn)\n\n```\nclass Heap {\n    constructor(stones) {\n        this.heap = stones;\n        this.size = stones.length;\n        this.heapify(0);\n    }\n    right(pos) {\n        return 2 * pos + 2;\n    }\n    left(pos) {\n        return 2 * pos + 1;\n    }\n    isleaf(pos) {\n        if (2 * pos + 1 >= this.size) return true;\n        return false;\n    }\n    swap(a, b) {\n        let temp = this.heap[a];\n        this.heap[a] = this.heap[b];\n        this.heap[b] = temp;\n    }\n    fix(pos) {\n        if (this.isleaf(pos)) return;\n        let left = this.left(pos);\n        let right = this.right(pos);\n        let bigger = left;\n        if (right < this.size)\n            bigger = this.heap[left] > this.heap[right] ? left : right;\n        if (this.heap[pos] < this.heap[bigger]) {\n            this.swap(pos, bigger);\n            this.fix(bigger);\n        }\n    }\n    heapify(pos) {\n        if (this.isleaf(pos)) return;\n        this.heapify(this.left(pos));\n        this.heapify(this.right(pos));\n        this.fix(pos);\n    }\n    delete() {\n        this.swap(0, --this.size);\n        this.fix(0);\n        return this.heap[0];\n    }\n    insert(val) {\n        this.size++;\n        this.heap[this.size - 1] = val;\n        this.heapify(0);\n    }\n    peek() {\n        return this.heap[0];\n    }\n}\n/**\n * @param {number[]} stones\n * @return {number}\n */\nvar lastStoneWeight = function (stones) {\n    const heap = new Heap(stones);\n    while (heap.size > 1) {\n        let x = heap.peek();\n        heap.delete();\n        let y = heap.peek();\n        heap.delete();\n        const res = x - y;\n        if (res > 0) heap.insert(res);\n    }\n    if (heap.size) return heap.peek();\n    return 0;\n}\n```
['Heap (Priority Queue)', 'JavaScript']