[Java/C++/Python] Priority Queue
Explanation\nPut all elements into a priority queue.\nPop out the two biggest, push back the difference,\nuntil there are no more two elements left.\n\n\n# Comp
# **Explanation**\nPut all elements into a priority queue.\nPop out the two biggest, push back the difference,\nuntil there are no more two elements left.\n<br>\n\n# **Complexity**\nTime `O(NlogN)`\nSpace `O(N)`\n<br>\n\n**Java, PriorityQueue**\n```java\n    public int lastStoneWeight(int[] A) {\n        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b)-> b - a);\n        for (int a : A)\n            pq.offer(a);\n        while (pq.size() > 1)\n            pq.offer(pq.poll() - pq.poll());\n        return pq.poll();\n    }\n```\n\n**C++, priority_queue**\n```cpp\n    int lastStoneWeight(vector<int>& A) {\n        priority_queue<int> pq(begin(A), end(A));\n        while (pq.size() > 1) {\n            int x = pq.top(); pq.pop();\n            int y = pq.top(); pq.pop();\n            if (x > y) pq.push(x - y);\n        }\n        return pq.empty() ? 0 : pq.top();\n    }\n```\n**Python, using heap, O(NlogN) time**\n```py\n    def lastStoneWeight(self, A):\n        h = [-x for x in A]\n        heapq.heapify(h)\n        while len(h) > 1 and h[0] != 0:\n            heapq.heappush(h, heapq.heappop(h) - heapq.heappop(h))\n        return -h[0]\n```\n\n**Python, using binary insort, O(N^2) time**\n```py\n    def lastStoneWeight(self, A):\n        A.sort()\n        while len(A) > 1:\n            bisect.insort(A, A.pop() - A.pop())\n        return A[0]\n```
[]