Hank Schrader's Rock Collection
IntuitionYo, dude, like, okay—so we gotta smash the biggest rocks first, right? But how the heck do we always grab the heaviest ones without checkin’ the whole
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Yo, dude, like, okay—so we gotta smash the biggest rocks first, right? But how the heck do we always grab the heaviest ones without checkin’ the whole list every time? Oh snap, a max-heap! But wait, Python’s like, “Nah, bro, I only got min-heaps.” No sweat, yo—just flip the numbers to negatives. Genius, right? Now the “smallest” negative is actually the biggest stone. Science, b****!
# Approach
<!-- Describe your approach to solving the problem. -->
Alright, check it:
1. One rock? Just roll with it, man. If there’s only one stone left, that’s your answer. No brainer.
2. Flippin’ negatives for that max-heap vibe. Turn all stones negative so Python’s min-heap acts like a max-heap. Sneaky, sneaky.
3. Smash ’em till there’s nothin’ left. Keep poppin’ the two biggest (well, the “smallest” negatives, but whatever). If they ain’t equal, chuck the leftover (y - x) back into the heap. Rinse and repeat.
4. What’s left? If there’s a stone chillin’ at the end, that’s your guy. Otherwise, peace out with a 0.
# Complexity
- **Time: O(n log n)**, yo. ‘Cause every pop and push is like O(log n), and we’re doin’ that for all the stones. Math, man.

- **Space: O(n)**, ‘cause the heap’s gotta hold all them stones. Simple enough.

# Code
```python3 []
class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        heapify(stones := [-s for s in stones])

        while len(stones) > 1:
            L, S = -heappop(stones), -heappop(stones)
            if L != S: heappush(stones, -(L - S))
                
        return -stones[0] if stones else 0
```
```python3 [FOR EXPLANATION CLICK HERE]
class Solution:  
    def lastStoneWeight(self, stones: List[int]) -> int:  
        # Only one rock? Yeah, we done here.  
        if len(stones) == 1:  
            return stones[0]  
          
        # Flip 'em negatives to trick the heap, yo  
        stones = [-stone for stone in stones]  
        heapq.heapify(stones)  # Boom, max-heap vibes  
          
        # Smash rocks like it’s a Breaking Bad montage  
        while len(stones) > 1:  
            y = -heapq.heappop(stones)  # Biggest rock (but negative, so shhh)  
            x = -heapq.heappop(stones)  # Second biggest  
              
            if x != y:  
                new_stone = y - x  
                heapq.heappush(stones, -new_stone)  # Toss the leftover back in  
          
        # What’s left? A hero or zero.  
        return -stones[0] if stones else 0  
```
---
᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼᲼***Stay cookin’***
![jesse stone.webp](https://assets.leetcode.com/users/images/0fa97f37-0ebe-4cfa-bd04-3526e8caae4d_1738274539.9531267.webp)

['Heap (Priority Queue)', 'Python3']