Typescript || Array || Heap
Approach\n Describe your approach to solving the problem. \nWe initialize a maximum priority queue which ensures the element we call each time has the maximum w
# Approach\n<!-- Describe your approach to solving the problem. -->\nWe initialize a maximum priority queue which ensures the element we call each time has the maximum weight. \n\nWe then insert all stones into this queue. As far as we have at least 2 stones, we pop out the two stones, if they are equal in weight we do nothing, else we get the difference and push back into the queue.\n\nWe then return the weight of the last stone left or 0 if none.\n\n# Complexity\n- Time complexity: $$O(n . logn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction lastStoneWeight(stones: number[]): number {\n    const maxPQueue = new MaxPriorityQueue();\n\n    stones.map(stone => maxPQueue.enqueue(stone));\n\n    while(maxPQueue.size() > 1) {\n        let first: number = maxPQueue.dequeue().element;\n        let second: number = maxPQueue.dequeue().element;\n\n        if(first !== second) {\n            maxPQueue.enqueue(first - second);\n        }\n    }\n\n    return maxPQueue.dequeue()?.element ?? 0;\n};\n```
['Array', 'Heap (Priority Queue)', 'TypeScript']