🔥Easy 🔥Java 🔥Soluton using 🔥PriorityQueue with 🔥Explanation/Intuition
PLEASE UPVOTE\n\n\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nUsing Priority Queue to sort at every operation.\n\n# Approach
# PLEASE UPVOTE\n\n![Screenshot 2023-04-24 at 06.29.54.png](https://assets.leetcode.com/users/images/877f6529-7103-45dc-a713-d2c71c40566d_1682298023.9539728.png)\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing Priority Queue to sort at every operation.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Inserting the whole array to Priorty Queue,\n2. Popping the first two elements from the pq (MAX and second MAX),\n3. adding the difference back to pq,\n4. repeating 2 & 3.\n5. return max when loop completes.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(NlogN)\n*sorting at every input and operation.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n\n# Code\n```\nclass Solution {\n    public int lastStoneWeight(int[] stones) {\n        PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());\n        for(int i=0;i<stones.length;i++){\n            pq.add(stones[i]);\n        }\n        int p,q;\n        while(pq.size()>1){\n            p=pq.poll();\n            q=pq.poll();\n            // System.out.println(\xF7p+" "+q);\n            pq.add(p-q);\n        }\n        return pq.poll();\n        \n    }\n}\n```
['Java']