C++ Explained 🔥 Easy Solution 🔥Priority Queue 🔥
PLEASE UPVOTE \uD83D\uDC4D\n# Intuition\n- #### To solve this problem, we can use a priority queue to keep track of the heaviest stones. \n- #### At each turn, 
# **PLEASE UPVOTE \uD83D\uDC4D**\n# Intuition\n- #### To solve this problem, we can use a priority queue to keep track of the heaviest stones. \n- #### At each turn, we can pop the two heaviest stones from the heap, smash them together according to the given rules, and then push the resulting stone (if any) back onto the heap. \n- #### We repeat this process until there is at most one stone left in the heap.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- #### In this code, we first create the priority queue by negating the values of the stones. \n- #### We then loop until there is at most one stone left in the heap. Inside the loop, we pop the two heaviest stones from the queue and check whether they are equal or not.\n    1. #### If they are not equal, we calculate the weight of the resulting stone and push it back onto the Queue (priority queue heapify itself after every push operation).\n- #### Finally, we return the weight of the last remaining stone (or 0 if there are no stones left).\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(NLogN)\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# **PLEASE UPVOTE \uD83D\uDC4D**\n# Code\n```\nclass Solution {\npublic:\n    int lastStoneWeight(vector<int>& a) {\n        priority_queue<int>pq(a.begin(),a.end());\n\n        while(pq.size() > 1)\n        {\n            int a = pq.top();\n            pq.pop();\n            int b = pq.top();\n            pq.pop();\n\n            if(a != b)\n                pq.push(abs(a-b));\n        }\n        return pq.empty() ? 0 : pq.top();\n    }\n};\n```\n![e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png](https://assets.leetcode.com/users/images/8044a3f7-5e30-4954-bb50-1485656ecab5_1682302010.1464121.png)\n
['Array', 'Queue', 'Heap (Priority Queue)', 'C++']