πͺ¨ Heaps: Last Stone Weight
π Problem Description
Given an array of stone weights, repeatedly smash the two heaviest stones together. If \(x == y\), both are destroyed; if \(x < y\), \(x\) is destroyed and \(y\) becomes \(y - x\). Return the weight of the last stone remaining, or 0.
Real-World Application
Priority Queues (Heaps) are essential for: - Task Scheduling: Prioritizing jobs in operating systems. - Bandwidth Management: Prioritizing critical network traffic. - Simulation: Event-driven simulations (e.g., waiting lines in banks).
π οΈ Constraints & Edge Cases
- \(1 \le \text{stones.length} \le 30\)
- \(1 \le \text{stones[i]} \le 1000\)
- Edge Cases to Watch:
- No stones left (return 0).
- Only one stone left (return its weight).
- Two stones of equal weight.
π§ Approach & Intuition
The Aha! Moment
Since we always need the heaviest two stones, a Max-Heap is ideal. Pythonβs heapq is a Min-Heap, so negate the weights to simulate a Max-Heap efficiently.
π’ Brute Force (Naive)
Sorting the array after every smash (\(O(N^2 \log N)\)), which is highly inefficient for frequent updates.
π Optimal Approach
Use a Max-Heap to manage stone weights.
1. Build a Max-Heap from the stones list.
2. While the heap size \(> 1\):
- Pop the two heaviest stones, \(y\) and \(x\).
- If \(y > x\), push \(y - x\) back onto the heap.
3. If the heap is empty, return 0, otherwise return the top element.
π§© Visual Tracing
graph LR
H["Heap: [8, 7, 4, 2, 1]"] -->|Smash 8, 7| S["8-7 = 1"]
S -->|Push 1| H2["Heap: [4, 2, 1, 1]"]
style H stroke:#333,stroke-width:2px
π» Solution Implementation
β±οΈ Complexity Analysis
- Time Complexity: \(\mathcal{O}(N \log N)\) β Each
heappopandheappushis \(\mathcal{O}(\log N)\), performed \(N\) times. - Space Complexity: \(\mathcal{O}(N)\) β To store the stones in the heap.
π€ Interview Toolkit
- Harder Variant: Implement the Heap from scratch with
bubbleUp/bubbleDownoperations. - Alternative Data Structures: Balanced BST (like
std::setin C++), but slower than a heap for this specific use case.
π Related Problems
- Kth Largest Element in an Array β Fundamental heap application.
- Task Scheduler β Complex task management using heaps.