Using Heap- 1ms | JAVA ✅ | beat 💯
\n# Complexity\n- Time complexity:\nO(log n)\n\n# Code\n\nclass Solution {\n    public int lastStoneWeight(int[] stones) {\n        PriorityQueue<Integer> pq=ne
\n# Complexity\n- Time complexity:\nO(log n)\n\n# Code\n```\nclass Solution {\n    public int lastStoneWeight(int[] stones) {\n        PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());\n        for(int i:stones)\n            pq.add(i);\n\n        int x;\n        int y;\n\n        while(pq.size()>1){\n\n             x=pq.poll();\n             y=pq.poll();\n\n            if(x>y)\n                pq.add(x-y);\n\n            }\n            \n        return pq.isEmpty()?0:pq.poll();\n    }\n}\n```\n![478xve.jpg](https://assets.leetcode.com/users/images/1296130b-2e6f-4f1a-b8dc-344f9b151589_1677230001.423075.jpeg)\n
['Array', 'Queue', 'Heap (Priority Queue)', 'Java']