✅ Easy Java solution || using maxHeap 🏆 || Beginner Friendly 🔥
Intuition\nNeed to maitain a DS to get top-2 stone, PriorityQueue is best for this\n\n# Approach\n1. Push all elments in prioriyQueue as MaxHeap.\n2. Pick top 2
# Intuition\nNeed to maitain a DS to get top-2 stone, PriorityQueue is best for this\n\n# Approach\n1. Push all elments in prioriyQueue as MaxHeap.\n2. Pick top 2 elements form maxHeap, if there is only one element left then that is answer.\n3. If both element are same, no action needed and continue the process.\n4. If both element are different, push the differece again.\n5. Answer will be if only one stone is left or no stone is left, **In case of only one stone is left, that is the answer, if no stoner are left, 0 is the answer.**\n# Complexity\n- Time complexity: $$O(n)$$ \n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution {\n    public int lastStoneWeight(int[] stones) {\n        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((stone1, stone2) -> stone2 - stone1);\n\n        for(int stone: stones) {\n            maxHeap.add(stone);\n        }\n\n        while(!maxHeap.isEmpty()) {\n            // take haviest stone\n            int y = maxHeap.poll();\n            if(maxHeap.isEmpty()) {\n                // if the was only one stone left, so that is answer\n                return y;\n            } else {\n                // take second haviest stone\n                int x = maxHeap.poll();\n                // if both are no equal then push the difference, no action needed for equal\n                if(x != y) {\n                    maxHeap.add(y - x);\n                }\n            }\n        }\n\n        // if no stone left in heap\n        return 0;\n\n    }\n}\n```
['Heap (Priority Queue)', 'Java']