↓Skip to main content
  1. LeetCode/

LeetCode 1046: Last Stone Weight

·2 mins· ·
LeetCode Easy Array Heap-Priority-Queue
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-30

  • Submissions: Wrong Answer → Runtime Error → Accepted → Runtime Error → Wrong Answer → Accepted → Runtime Error → Accepted ×2

Approach
#

Each round needs the two heaviest stones, so use a max-heap (Heap; negate values in Python). Take y >= x and push back y - x if they differ. With negated values, mind the sign: for the popped negatives a (heavier, popped first) and b, a - b is negative, exactly the negated difference to push. Return 0 if the heap empties.

My Attempts
#

I wrote this four times (2022-09, twice in 2022-11, 2025) and failed 5 times:

  • The first 2022-09 version pushed back x - y, which is positive since the values are negated, so it became a very light stone: [2, 7] returned -5 instead of 5. With y - x, two equal stones cancelling everything made h[0] go out of range (IndexError).
  • A 2022-11 rewrite inverted the condition as if heaviest != second_heaviest: continue, discarding exactly the unequal pairs: [2, 7] returned 0. The other two failures were IndexErrors on an empty heap.

Solution
#

class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        h = [-s for s in stones]
        heapq.heapify(h)
        while len(h) > 1:
            a = heapq.heappop(h)
            b = heapq.heappop(h)
            if a != b:
                heapq.heappush(h, a - b)
        return -h[0] if h else 0

Complexity
#

Time O(n log n), space O(n).

Mistakes to Avoid
#

  • With negated values, push a negative difference.
  • The heap may end up empty; return 0.

Interview Explanation
#

Each round needs the two heaviest, so I pop two from a negated max-heap, push back their difference when unequal, and return the last stone or 0. Time O(n log n).

Related

LeetCode 506: Relative Ranks
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 506 note: sort indices by score descending, give medals to the top three and rank strings to the rest, writing back into the original positions
LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 1002: Find Common Characters
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 1002 note: count each word and intersect all Counters (minimum count per character), then expand
LeetCode 1005: Maximize Sum Of Array After K Negations
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 1005 note: negate the smallest value each time; once the smallest is positive, only the parity of the remaining count matters
LeetCode 1013: Partition Array Into Three Parts With Equal Sum
·2 mins
LeetCode Easy Array Greedy
LeetCode 1013 note: the total must be divisible by 3; accumulate from the left, cutting whenever the running sum hits total / 3, and succeed with at least 3 pieces
LeetCode 1018: Binary Prefix Divisible By 5
·1 min
LeetCode Easy Array Bit-Manipulation
LeetCode 1018 note: each new bit turns the prefix into x = 2x + bit, so keep only x mod 5 and the number never grows